this is my code and from toolstrip click event i want to call the menustrip sub items
Ex: Menu like : Settings -> User. I want to call user_click event from toolstip click
private void tbrIUC1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string menuname="mnuuser";
e.ClickedItem.Click += new EventHandler(menuname + "_Click");
}
You can use anonymous delegate:
private void tbrIUC1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string menuname="mnuuser";
e.ClickedItem.Click += (s, ev) => { your code when clicked };
}
From another stackoverflow post I got the following working for me:
ButtonName.Click += (se, ev) => button1_Click(se, ev);
Link to "inspiration" How can I create a dynamic button click event on a dynamic button?
Look for the answer from A9S6 and the comment to this from Scott Beeson
EDIT
To call a function BASED on the function name (string) I got the following article:
http://www.vcskicks.com/call-function.php
I haven't got it working for me by now but I wanted to share a possible solution
If all fails: I recommend declaring a new method with a Switch that gets the control and the string passed
Related
I want to add an event to a programmatically generated button like this:
Button activityButton = new Button();
activityButton.Click += new EventHandler(onChangeActivityFilter);
I'm getting the following exception in the 2nd line:
Cannot implicit convert type System.EventHandler to System.Windows.RoutedEventhandler
The onChangeActivityFilter methode looks like this:
private void onChangeActivityFilter(object sender, EventArgs e)
{
}
I'd like to know what I'm doing wrong.
You need to create a instance of RoutedEventHandler:
activityButton.Click += new RoutedEventhandler(onChangeActivityFilter);
And also change the method signature:
private void onChangeActivityFilter(object sender, RoutedEventArgs e)
{
}
RoutedEvents where introduced with WPF.
You can also use lambda functions
activityButton.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};
This can seems like a simple question ... the crux is how to match the button delegate signature void, object, eventargs with my method or use an event delegate.
As an example, I have code for a button that changes color when it's clicked. However,
button1.Click += new EventHandler(KK.ChangeColor);
carries the EventArgs from the button to the ChangeColor(object sender, EventArgs e) method, but is meaningless to the rest of the code which use ColorEventArgs; and
button1.Click += delegate(object sender, EventArgs e){ KK.ChangeColor(sender); };
doesn't allow for later removal of the delegate later in the code.
So which is better? Adding unnecessary parameters to all my methods to match the button delegate or suffering from not being able to remove the delegate later ?
or How would I change the delegate signature of the button? It seems there must be a 'cleaner' way to do this?
Will appreciate advice.
"It seems there must be a 'cleaner' way to do this?"
In my opinion, better design would depends on what exactly ChangeColor method do. If it is doing only specific operation that closely related to event button clicked, I would just leave it as the real event handler method. That means, it should have required parameters to match Button.Click event handler signature (I don't think there is option to "change the delegate signature") :
void ChangeColor(object sender, EventArgs e)
{
MessageBox.Show("Button {0} clicked!!!", ((Button)sender).Name);
}
Otherwise, if it is doing not only specific operation related to event button clicked, I would refactor codes unrelated to button click event to another method. This way, the other method doesn't need to have unnecessary parameters. For example :
void ChangeColor(object sender, EventArgs e)
{
var buttonName = ((Button)sender).Name;
MessageBox.Show("Button {0} clicked!!! Save form data", );
//assume that form name can be determined from name of button being clicked
SaveFormToDatabase(buttonName);
}
private void SaveFormToDatabase(string formName)
{
//save form specified in parameter
}
I just started programming, and I want to use WinForms to make multiple buttons that you can click on to change from white to lime-green and back to white. I have done this for one button:
private void button1_Click(object sender, EventArgs e)
{
if (button1.BackColor != Color.Lime)
{
button1.BackColor = Color.Lime;
}
else
{
button1.BackColor = Color.White;
}
}
Now I could copy and paste that for all of the buttons, but I know that is inefficient; and if I use winforms to reference button1 on button2, it will just change the color of button1 (obviously).
So, do I need to use a helper method, new class, or something else? What would that look like?
There are a couple of approaches. One might be to create a common function which the different buttons call:
private void button1_Click(object sender, EventArgs e)
{
ChangeColor(button1);
}
private void ChangeColor(Button button)
{
if (button.BackColor != Color.Lime)
button.BackColor = Color.Lime;
else
button.BackColor = Color.White;
}
Then each button handler can use that same function call.
Or, if all of these buttons will always ever do exactly the same thing, then you can use one click handler function for all of them. In this case what you'd need to do is determine which button invoked the handler (whereas you're currently referencing button1 directly) so that you know which one to change. The sender object passed into the handler function is actually a reference to the form element which invoked the handler. All you need to do is cast it:
private void button_Click(object sender, EventArgs e)
{
var button = (Button)sender;
if (button.BackColor != Color.Lime)
button.BackColor = Color.Lime;
else
button.BackColor = Color.White;
}
So first the handler grabs a reference to the button which invoked it, then runs the logic on that button. Note also how I made the name of the handler function slightly more generic. Now you'd go to the form designer and set button_Click as the click handler for all of the buttons which should invoke this.
You do this the exact same way you'd do it for any C# class. You derive your own class and customize the base class behavior. Every event has a corresponding OnXxxx() method that you can override.
Add a new class to your project and paste this code:
using System;
using System.Windows.Forms;
class MyButton : Button {
protected override void OnClick(EventArgs e) {
// Your code here
//...
base.OnClick(e);
}
}
Change the code in OnClick() to do what you want to do. Compile. You'll now have your own button control on the top of the toolbox. And can drop as many copies of it as you want on a form. They'll all behave the same without having to add any code in the form.
Probably the easiest way would be to have each button invoke the same click handler. Then inside of your handler use the Sender instead of hard coding Button1.
private void buttons_Click(object sender, EventArgs e)
{
var theButton = (Button) sender;
if (theButton.BackColor != Color.Lime)
{
theButton.BackColor = Color.Lime;
}
else
{
theButton.BackColor = Color.White;
}
}
You can get the button that raised the Click event by casting sender to Button.
You can then add the same handler to every button.
I'm a VB guy.... in VB.Net you can add multiple handlers for events and connect multiple events to the same handler.
This sub hooks all clicks to color the buttons.
Private Sub ColorButtons(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click, Button2.Click, ..
I do this all the time accidentally because I drag/copy a control to make a new one and the new button gets added to the original's events.
Other Subs can handle the same events to do other work - both will execute.
No idea how to do this in C#.
The proper way to do this really is to associate each button's click event to the function you have coded for that purpose (you want the function to run when the button is clicked, right?), so add the following (or similar) to an appropriate section of your code:
MyButton1.Click += new RoutedEventHandler(buttons_Click);
MyButton2.Click += new RoutedEventHandler(buttons_Click);
etc...
You can associate as many controls to the event handler as you like.
What I usually do before is this:
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
This code will just simply run the codes under button1_Click.
But try not to practice as such and just simply put it in a function/method just like what David suggested.
If I have a button which does something and also a double-click event on a data grid which I want to do the same thing, what is the best way to ensure that only one function has to be maintained?
Apart from doing the following, is there any fancy C# way to indicate that two events are to do the same thing?
void button1_Click(...) { MyFunction(); }
void dataGrid1_DoubleClick(...) { MyFunction(); }
void MyFunction() { // do stuff }
I suppose that you are talking about a DataGridView (WinForms) so the signature of the event DoubleClick in the DataGridView and the signature of Click event on a button control is the same.
(An EventHadler). In this case you can simply set the same method using the form designer or manually bind the event
dataGridView1.DoubleClick += new EventHandler(MyFunction);
button1.Click += new EventHandler(MyFunction);
Of course the MyFunction method should match the expected signature of an EventHandler
private void MyFunction(object sender, EventArgs e)
{
// do your work
}
Reviewing my answer after a few minutes I wish to add:
If you find yourself in a situation in which you need to differentiate between the controls using the sender object (like Control c = sender as Control; if (c.Name == "someName") ) I really suggest you to return to the first idea. Call a common method but keep the EventHandler separated for each control involved.
Using VS, in the form's designer view You can set the procedure You want to call to each control's each event in the control's properties window.
image
Just to add to what Steve said, you will want to bind these events to your function manually in the Load event of your form, instead of using the events under the lightning bolt in the properties window in the designer, like so:
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += MyMethod;
dataGridView1.DoubleClick += MyMethod;
}
void MyMethod(object sender, EventArgs e)
{
//Do Stuff
}
Also, declaring a new instance of the EventHandler class has been redundant since Anonymous methods were introduced to C#, you can just point the event directly at the method as shown above.
I have DataList which contains different fields. One of them is next DataList. This inner DataList contains button. And now I would like to bind data for the inner DataList in code behind in OnItemDataBound method. And I need to write there delegate for button. How can I do it? I find the button as the follow:
((Button)e.Item.FindControl("btn_down"))
And now I would like somehow to define what it should do
And it's important I don't want to use:
((Button)e.Item.FindControl("btn_down")).Click +=new EventHandler(btn_Click);
as I need to use some data from OnItemDataBound in this 'Click' function
((Button)e.Item.FindControl("btn_down")).Click +=new EventHandler(btn_Click);
private void btn_Click(object sender, EventArgs e)
{
}
Edit, if you need a custom event handler instead of the default one:
((Button)e.Item.FindControl("btn_down")).Click += new EventHandler(delegate(Customer Parameters Here) {});
private void btn_Click(Customer Parameters Here)
{
}
Ok,
I found a solution:
((Button)e.Item.FindControl("btn_up")).Click += new EventHandler(delegate(object s, EventArgs args) {});