How make one event handler that applies to multiple controls in C#? - c#

In Visual Basic I knew how to do it, but I'm new to C#, so can you guys tell me how do I make a "private void" with mouse hover that applies the same event to multiple controls? There's an example:
private void button1, button2, button3, button4_MouseHover(object sender, EventArgs e)
{
btn.Image = pic
}

Just declare one event handler and point each button at it:
private void Common_MouseHover(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn != null)
btn.Image = pic
}
Then in code or designer:
button1.MouseHover += Common_MouseHover;
button2.MouseHover += Common_MouseHover;
// .. etc

When you subscribe to the event on a button, it's just a standard event handler:
button1.Click += myEventHandler;
You can use the same code to add handlers for every button:
button1.Click += myEventHandler;
button2.Click += myEventHandler;
button3.Click += myEventHandler;
button4.Click += myEventHandler;
button5.Click += myEventHandler;
button6.Click += myEventHandler;

button1.MouseOver += OnMouseOver(...)
button2.MouseOver += OnMouseOver(...)
button3.MouseOver += OnMouseOver(...)
button4.MouseOver += OnMouseOver(...)

Related

How to create and call dynamically instantiated methods in C#? [duplicate]

I am creating one button on a page dynamically. Now I want to use the button click event on that button.
How can I do this in C# ASP.NET?
Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);
//protected void button_Click (object sender, EventArgs e) { }
The easier one for newbies:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
Simply add the eventhandler to the button when creating it.
button.Click += new EventHandler(this.button_Click);
void button_Click(object sender, System.EventArgs e)
{
//your stuff...
}
It is much easier to do:
Button button = new Button();
button.Click += delegate
{
// Your code
};
You can create button in a simple way, such as:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
But event probably will not fire, because the element/elements must be recreated at every postback or you will lose the event handler.
I tried this solution that verify that ViewState is already Generated and recreate elements at every postback,
for example, imagine you create your button on an event click:
protected void Button_Click(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) != "true")
{
CreateDynamicElements();
}
}
on postback, for example on page load, you should do this:
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) == "true") {
CreateDynamicElements();
}
}
In CreateDynamicElements() you can put all the elements you need, such as your button.
This worked very well for me.
public void CreateDynamicElements(){
Button button = new Button();
button.Click += new EventHandler(button_Click);
}
Let's say you have 25 objects and want one process to handle any one objects click event. You could write 25 delegates or use a loop to handle the click event.
public form1()
{
foreach (Panel pl in Container.Components)
{
pl.Click += Panel_Click;
}
}
private void Panel_Click(object sender, EventArgs e)
{
// Process the panel clicks here
int index = Panels.FindIndex(a => a == sender);
...
}

How can I get a simple way of verifying if a button is clicked, and if it is, change it's text?

I have a panel where I have a lot of buttons. Is there a way I can check if any of those buttons are clicked, and if it is, change the text from that specific button?
I've been looking around and I've got till now is this
foreach (Control button in panel1.Controls)
{
if ( button.Click == ??? ) //I can't use '==',
//but I don't know what to do here
{
//changing the text would happen here
}
}
Can anyone help me out here?
You could let all buttons use the same click-event handler:
protected void Button_Clicked(object sender, EventArgs e)
{
((Button) sender).Text = "Insert Text Here";
}
In your constructor:
this.Button1.Click += new System.EventHandler(this.Button_Clicked);
this.Button2.Click += new System.EventHandler(this.Button_Clicked);
The easiest thing you can do is to attach the same event handler to all of your Buttons Click event inside of your Panel.
var buttons = panel1.Controls.OfType<Button>();
foreach(var btn in buttons)
btn.Click += Button_Click;
private void Button_Click(object sender, EventArgs e)
{
var btn = sender as Button;
if(btn != null) btn.Text = "Something else";
}
Or you can do it with lambda statement instead of declaring separate method:
var buttons = panel1.Controls.OfType<Button>();
foreach(var btn in buttons)
btn.Click += (s,e) => ((Button)s).Text = "Clicked";

One function for two events

I have this code that I attach to the DoubleClick event on the Tray Icon for my app:
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
this.MainWindow.Show();
};
But, is it possible to use this code for two events (DoubleClick and Click), like so:
ni.DoubleClick, ni.Click +=
delegate(object sender, EventArgs args)
{
this.MainWindow.Show();
};
Just for minimalize a code size and readability. Thanks
Put the handler in its own function:
private void ClickHandler(object sender, EventArgs args)
{
this.MainWindow.Show();
}
Then wire it up to both events:
ni.DoubleClick += ClickHandler;
ni.Click += ClickHandler;
Just create EventHandler using lambda expression and add it to both event.
EventHandler e = (sender, args) => this.MainWindow.Show();
ni.DoubleClick += e;
ni.Click += e;
No, but you can make it a standard, non anonymous function and use it for both events.
private void OnClick(object sender, EventArgs e) { ... }
ni.DoubleClick += OnClick;
ni.Click += OnClick;
Just assign it to a variable beforehand:
EventHandler eventHandler = delegate(object sender, EventArgs args)
{
this.MainWindow.Show();
};
ni.DoubleClick += eventHandler;
ni.Click += eventHandler;
BTW, the event handler definition can be simplified using the anonymous method syntax:
EventHandler eventHandler = (s, e) => this.MainWindow.Show();

How to Use Dynamically Created Button/textBox in C# Windows Forms?

private void createButton()
{
flowLayoutPanel1.Controls.Clear();
for (int i = 0; i < 4; i++)
{
Button b = new Button();
b.Name = i.ToString();
b.Text = "Button" + i.ToString();
flowLayoutPanel1.Controls.Add(b);
}
}
private void button1_Click(object sender, EventArgs e)
{
createButton();
}
I Used this code to create some buttons on runtime , now how can i use those created buttons to perform diffrent actions? Im kindz new to this so please help me , very much appreciated :)
You can assign an event handler to the click event:
b.Click += SomeMethod;
SomeMethod must have the following signature:
void SomeMethod(object sender, EventArgs e)
b.Click += delegate(object sender, EventArgs e) {
Button clickedButton = (Button)sender; //gets the clicked button
});
When you create your button, you need to subscribe to the Click event like this :
Button b = new Button();
b.Click += new EventHandler(b_Click);
// or
b.Click += b_Click;
// or
b.Click += delegate(object sender, EventArgs e) {/* any action */});
// or
b.Click += (s, e) => { /* any action */ };
void b_Click(object sender, EventArgs e)
{
// any action
}
This is something that is automatically done when your are is the designer in Visual Studio, and you click on a button to create the method button1_Click.
You can search in the Designer.cs of your form, you will find an equivalent line:
button1.Click += new EventHandler(button1_Click);
Related question:
How can i create dynamic button click event on dynamic button

Adding a Click event handler programmatically to a radio button

I know for a button, I can do this:
this.button1.Click += new System.EventHandler(this.button_Click);
But how can I do it for a radio button?
How about:
radioButton1.Click += new RoutedEventHandler(radioButton1_Click);
private void radioButton1_Click(object sender, RoutedEventArgs e)
{
}
this.button1.Checked += ...
this.button1.Unchecked += ...

Categories