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 += ...
Related
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);
...
}
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";
I want to put on existing button new event handle. I have 9 buttons created in form and want to display each name when is clicked on it, but don't want to put on every button message manually.
How to accomplish that?
You can use the sender propperty of the EventHandler in order to do that. This example uses winforms buttons:
SomeButtonClicked(object sender, EventArgs e)
{
var button = sender as Button;
MessageBox.Show(button.Name);
}
And then you can add this event to multiple buttons i.e.:
button1.Click += new System.EventHandler(SomeButtonClicked);
button2.Click += new System.EventHandler(SomeButtonClicked);
button3.Click += new System.EventHandler(SomeButtonClicked);
You can code something like this
private void MainForm_Load(object sender, EventArgs e) {
...
// Assigning on click event, assuming that all your buttons are on the MainForm
foreach (Control ctrl in Controls) {
Button btn = ctrl as Button;
if (!Object.ReferenceEquals(null, btn))
btn.Click += onButtonClick;
}
...
// On click itself
private void onButtonClick(Object sender, EventArgs e) {
Button btn = sender as Button;
String name = btn.Name; // <- Or whichever property of the button you want
}
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
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(...)