Activate tool strip click event from another form - c#

I need to send a click event to refreshToolStripMenuItem from another form. Here is what I have, for some reason it doesn't work. Help please.
Menu item click:
public void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
noteslist.Items.Clear();
idlist.Items.Clear();
setnotes();
}
Code used to send event:
frmnotes notes = new frmnotes();
notes.refreshToolStripMenuItem_Click(this, e);
this.Close();

Dont call the event itself.
It's bad code.
Put the create an own protected void updateMyList() Method.
internal void updateMyList()
{
noteslist.Items.Clear();
idlist.Items.Clear();
setnotes();
}
Then call the update-method from within your event.
private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
updateMyList();
}
Then simply call the update-method from your form:
frmnotes notes = new frmnotes();
notes.updateMyList();
this.Close();
Btw.: Set the modifier of your Click events i.e. refreshToolStripMenuItem_Click to private.
You never should call them from outside the form.
Take a look at the MVC pattern for more info. It really helps.

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)

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

Triggering a "Click" event without user input

I have a Windows Forms Link Label, "Refresh", that refreshes the display.
In another part of my code, part of a separate windows form, I have a dialog that changes the data loaded into the display in the first place. After executing this other code, pressing "Refresh" updates the data correctly.
Is there a simple way for the dialog menu to "click" the "refresh" Link Label after it has finished altering the data?
Using Visual Studio 2008.
For button is really simple, just use:
button.PerformClick()
Anyway, I'd prefer to do something like:
private void button_Click(object sender, EventArgs e)
{
DoRefresh();
}
public void DoRefresh()
{
// refreshing code
}
and call DoRefresh() instead of PerformClick()
EDIT (according to OP changes):
You can still use my second solution, that is far preferable:
private void linkLabel_Click(object sender, EventArgs e)
{
DoRefresh();
}
public void DoRefresh()
{
// refreshing code
}
And from outside the form, you can call DoRefresh() as it is marked public.
But, if you really need to programmatically generate a click, just look at Yuriy-Faktorovich's Answer
You could call the PerformClick method. But Generally it is better to have the Click event of the button call a Refresh method you write. And the menu call that method as well. Otherwise your menu depends on the button being there.
Edit:
A LinkLabel implements the IButtonControl explicitly. So you could use:
((IButtonControl)button).PerformClick();
you can use a method to refrech display, the bouton_click and the dialogBox call this method
public void refrechDate()
{
}
private void button_click(...)
{
refrechData();
}
private void MyMethod()
{
// ...
// calling refresh
this.button1_Click(this.button1, EventArgs.Empty);
// ...
}
private void button1_Click(object sender, EventArgs e)
{
// refresh code
}

Closing a form and doing something on return to another form

I have a mainForm and a saveForm.
I never close the mainForm, just let the saveForm appear over the top.
When i close the saveForm, I want a piece of code to run on returning to mainForm.
What is the best way to achieve this?
In addition to #benPearce's answer, if you are content to have saveForm appear modally, then you can just call:
So in the mainForm, I am assuming you have a Save button (let's call it btnSave) of some kind that brings up saveForm, right? Right. So double click on that Save button and Visual Studio will create an event handler for you. Type in the code below.
private void btnSave_Click(object sender, EventArgs e)
{
saveForm sf = new SaveForm();
if (sf.ShowDialog() == DialogResult.OK)
{
// do your thing
}
}
Of course, you have to make sure that the saveForm is setting the DialogResult. For instance, assuming you have an OK button in the saveForm that is supposed to close the saveForm... In the Click event for the OK button you would do this:
private void btnOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
In mainForm, subscribe to the FormClosed event on the saveForm, put your code in the event handler for this event
void saveForm_FormClosed(object sender, FormClosedEventArgs e)
{
/// code here
}

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