Call an event containing parameter from another event in c# - c#

I want to call the event from within another event.
I want to call this event
private void gv_client_CellContentClick(object sender, DataGridViewCellEventArgs e){}
From this event as
private void update_staff_Click(object sender, EventArgs e){
//some codes
gv_client_CellContentClick(); // i want to call this event here
}

If the event is in same class you can call it like
private void update_staff_Click(object sender, EventArgs e){
//some codes
gv_client_CellContentClick(sender,e); // i want to call this event here
}

As per our comment thread, it seems you want to call the method (as opposed to raising the event).
In your original handler, you can simply call the method:
private void update_staff_Click(object sender, EventArgs e)
{
var rowIndex = ???;
var columnIndex = ???;
var args = new DataGridViewCellEventArgs(columnIndex, rowIndex);
gv_client_CellContentClick(sender, args); // Note: You might need to change sender too if you know this function uses it...
}
The bit you'll need to figure out is the row/column indexes. Presumably this can be retrieved based on the location of the "update_staff" button/control being clicked - tip: cast "sender" to whatever control type you know it is to work out which button/control was clicked.

Related

Call a function on CodeBehind that's operated by a LinkButton (ASP.net, C#)

Hi StackOverflow Community,
I have this code on CodeBehind:
protected void Btn_Search_Function(object sender, EventArgs e)
{
GV_Results.PageIndex = 0;
GV_Results.DataBind();
hdnSelectedTab.Value = "1";
}
This code is executed when I click a LinkButton. I want to call this function when another method (in the same page) finishes executing.
But I don't know what arguments to pass as object sender and EventArgs e. What is the best approach to achieve this?
Thank you in advance,
Best regards.
Ok so after 5 minutes I had the idea of creating a third method that would be called both by the function that is called when I click the LinkButton, and by the method I want to execute the same code.
I'm open to better ways of achieving this.
So, it is has follows:
protected void Btn_Search_Function(object sender, EventArgs e)
{
SearchFunction();
}
private void SearchFunction()
{
GV_Results.PageIndex = 0;
GV_Results.DataBind();
hdnSelectedTab.Value = "1";
}
If you have a button called Btn_Search and you have created an event handler for the button click event Btn_Search_Click(object sender, EventArgs e).
then, in your another method you can call like:
public void my_function()
{
//This simulates the button click from within your code.
Btn_Search_Click(Btn_Search, EventArgs.Empty);
}
or
Btn_Search_Click(null, EventArgs.Empty);
or for your function
Btn_Search_Function(null, EventArgs.Empty)

Call a function that has parameter type inside another function c#

i got this function on my form:
private void UpdateQuantityDataGridView(object sender, DataGridViewCellEventArgs e)
{
(...codes)
}
and i want to call that function inside another function, let's say when i click a "OK" button, this below function will run and execute above function that has parameter type.
private void button5_Click(object sender, EventArgs e) // This is the "OK" button click handler.
{
SubmitButton(sender, e);
}
private void SubmitButton(object sender, EventArgs e) // This is function of "OK" button
{
(...codes)
UpdateQuantityDataGridView("What should i put in here? I tried (sender, e), but it is useless")
}
I know that this function run when we put something like this:
dataGridView1.CellValueChanged += new DataGridViewSystemEventHandler(...);
But, i don't want that because that function will only run if the cell value in DataGridView has been changed, i want to access that function when i click "OK" button. But, what should i put inside a parameters value?
Extract the logic currently in the UpdateQuantityDataGridView() method and put it into a new public method named whatever you want, then you can call this logic from anywhere in your class or any other code that references your class, like this:
public void DoUpdateQuantityLogic()
{
// Put logic here
}
Note: If you do not actually use sender or e, then you can leave the method above without parameters, but if you do use e, for example, then you need to have a parameter for the DoUpdateQuantityLogic() method to account for what the property of the e object you are using is.
Now you can call DoUpdateQuantityLogic() from you other methods, like this:
private void button5_Click(object sender, EventArgs e) // This is the "OK" button click handler.
{
DoUpdateQuantityLogic();
}
private void SubmitButton(object sender, EventArgs e) // This is function of "OK" button
{
DoUpdateQuantityLogic();
}
This allows you to re-use your logic and also isolates the functionality into a method that makes unit testing easier, if you choose to unit test this logic.
If you are determined to use your existing event-based method infrastructure, then you can pass null for both the sender and the e arguments of the event handler, like this:
UpdateQuantityDataGridView(null, null);
If your method UpdateQuantityDataGridView() actually using the parameters sender and e? If not just pass null for both.
UpdateQuantityDataGridView(null, null);
If you are using them:
var e = new DataGridViewCellEventArgs();
// assign any properties
UpdateQuantityDataGridView(dataGridView1, e);
You can use sender, but you can't use e because UpdateQuantityDataGridView needs e to be of type DataGridViewCellEventArgs.
Depending on what your UpdateQuantityDataGridView handler wants to do with the e parameter, you could just pass null when you call it from your SubmitButton.
Otherwise, you'll have to new a DataGridViewCellEventArgs and populate it with the appropriate values your own handler requires/expects.

How to call treeView.SelectedItemChanged programmatically

In my program I would like to call to a SelectedItemChanged event using c# code-behind, I am just unsure about what to pass as parameters. This is for a TreeViewItem.
//Gets selected item in TreeView
private void TreeOne_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
MainWindowViewModel.SelectedItem = e.NewValue as TreeViewItem;
}
//I'm calling the SelectedItemChanged event from a RightButtonDown event
private void TreeOne_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
TreeOne_SelectedItemChanged(/* What would go here? **/);
}
Also, when I try to build this I receive this compiler error that pretty much led to this question...
No overload for method TreeOne_SelectedItemChanged takes '0' arguments
I'm hoping that this is an easy question, but if I have not provided enough information, or haven't been clear enough please let me know.
Adding to #Bart Friederichs' answer and assuming that you have a reference to your TreeView, you could add the following method:
private void SetSelectedItem()
{
MainWindowViewModel.SelectedItem = TreeOne.SelectedItem;
}
Then you can simply call this from wherever you like:
private void TreeOne_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
SetSelectedItem();
}
private void TreeOne_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
SetSelectedItem();
}
The usual design pattern would be to call some kind of processing method, and not to "manually" fire events:
private TreeOne_SelectedItemChaned(object sender,
RoutedPropertyChangedEventArgs<object> e) {
processChange();
}
Then, from withing your code, you just call processChange(), no need to call TreeOne_SelectedItemChanged.
try to call
TreeOne_SelectedItemChanged(null, null);

Trigger control's event programmatically

Assume that I have a WinFoms project. There is just one button (e.g. button1).
The question is: is it possible to trigger the ButtonClicked event via code without really clicking it?
Button controls have a PerformClick() method that you can call.
button1.PerformClick();
The .NET framework uses a pattern where for every event X there is a method protected void OnX(EventArgs e) {} that raises event X. See this Msdn article. To raise an event from outside the declaring class you will have to derive the class and add a public wrapper method. In the case of Button it would look like this:
class MyButton : System.Windows.Forms.Button
{
public void ProgrammaticClick(EventArgs e)
{
base.OnClick(e);
}
}
You can just call the event handler function directly and specify null for the sender and EventArgs.Empty for the arguments.
void ButtonClicked(object sender, EventArgs e)
{
// do stuff
}
// Somewhere else in your code:
button1.Click += new EventHandler(ButtonClicked);
// call the event handler directly:
ButtonClicked(button1, EventArgs.Empty);
Or, rather, you'd move the logic out of the ButtonClicked event into its own function, and then your event handler and the other code you have would in turn call the new function.
void StuffThatHappensOnButtonClick()
{
// do stuff
}
void ButtonClicked(object sender, EventArgs e)
{
StuffThatHappensOnButtonClick();
}
// Somewhere else in your code:
button1.Click += new EventHandler(ButtonClicked);
// Simulate the button click:
StuffThatHappensOnButtonClick();
The latter method has the advantage of letting you separate your business and UI logic. You really should never have any business logic in your control event handlers.
Yes, just call the method the way you would call any other. For example:
private void btnSayHello_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World!");
}
private void btnTriggerHello_Click(object sender, EventArgs e)
{
btnSayHello_Click(null, null);
}
button1.PerformClick();
But if you have to do something like this maybe it's better to move the code you have under the event on a new method ?
Why don't you just put your event code into a Method. Then have the Event execute the method. This way if you need to execute the same code that the Event rises, you can, but simply just calling the "Method".
void Event_Method()
{
//Put Event code here.
MessageBox.Show("Hello!");
}
void _btnSend_Click(object sender, EventArgs e)
{
Event_Method();
}
void AnotherMethod()
{
Event_Method();
}
Make sense? Now the "Click" event AND anywhere in code you can trigger the same code as the "Click" event.
Don't trigger the event, call the method that the event calls. ;)
In most cases you would not need to do that. Simply wrap your functionality in functions related to a specific purpose (task). You call this function inside your event and anywhere else it's needed.
Overthink your approach.
I recently had this problem where I wanted to programatically click a button that had multiple event handlers assigned to it (think UserControl or derived classes).
For example:
myButton.Click += ButtonClicked1
myButton.Click += ButtonClicked2;
void ButtonClicked1(object sender, EventArgs e)
{
Console.WriteLine("ButtonClicked1");
}
void ButtonClicked2(object sender, EventArgs e)
{
Console.WriteLine("ButtonClicked1");
}
When you click the button, both functions will get called. In the instances where you want to programmatically fire an event handler for a function from a form (for example, when a user presses enter in a Text field then call the InvokeOnClick method passing through the control you. For example
this.InvokeOnClick(myButton, EventArgs.Empty);
Where this is the Form instance you are in.
use a for loop to call the button_click event
private void btnadd_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i <= 2; i++)
StuffThatHappensOnButtonClick();
}
void StuffThatHappensOnButtonClick()
{
........do stuff
}
we assume at least one time you need click the button

How do I call an event method in C#?

When I create buttons in C#, it creates private void button_Click(object sender, EventArgs e) method as well.
How do I call button1_click method from button2_click?
Is it possible?
I am working with Windows Forms.
How do I call button1_click method
from button2_click? Is it possible?
Its wholly possible to invoke the button's click event, but its a bad practice. Move the code from your button into a separate method. For example:
protected void btnDelete_OnClick(object sender, EventArgs e)
{
DeleteItem();
}
private void DeleteItem()
{
// your code here
}
This strategy makes it easy for you to call your code directly without having to invoke any event handlers. Additionally, if you need to pull your code out of your code behind and into a separate class or DLL, you're already two steps ahead of yourself.
// No "sender" or event args
public void button2_click(object sender, EventArgs e)
{
button1_click(null, null);
}
or
// Button2's the sender and event args
public void button2_click(object sender, EventArgs e)
{
button1_click(sender, e);
}
or as Joel pointed out:
// Button1's the sender and Button2's event args
public void button2_click(object sender, EventArgs e)
{
button1_click(this.button1, e);
}
You don't mention whether this is Windows Forms, ASP.NET, or WPF. If this is Windows Forms, another suggestion would be to use the button2.PerformClick() method. I find this to be "cleaner" since you are not directly invoking the event handler.
You can wire up the button events in the ASPX file code.
The button tag will wire the events like this:
<asp:Button Text="Button1" OnClick="Event_handler_name1" />
<asp:Button Text="Button2" OnClick="Event_handler_name1" />
Just wire the OnClick= to your handler method for button1
You can bind same handler for the event of both buttons

Categories