how to handle programmatically added button events? c# - c#

I'm making a windows forms application using C#. I add buttons and other controls programmatically at run time. I'd like to know how to handle those buttons' click events?

Try the following
Button b1 = CreateMyButton();
b1.Click += new EventHandler(this.MyButtonHandler);
...
void MyButtonHandler(object sender, EventArgs e) {
...
}

Use this code to handle several buttons' click events:
private int counter=0;
private void CreateButton_Click(object sender, EventArgs e)
{
//Create new button.
Button button = new Button();
//Set name for a button to recognize it later.
button.Name = "Butt"+counter;
// you can added other attribute here.
button.Text = "New";
button.Location = new Point(70,70);
button.Size = new Size(100, 100);
// Increase counter for adding new button later.
counter++;
// add click event to the button.
button.Click += new EventHandler(NewButton_Click);
}
// In event method.
private void NewButton_Click(object sender, EventArgs e)
{
Button btn = (Button) sender;
for (int i = 0; i < counter; i++)
{
if (btn.Name == ("Butt" + i))
{
// When find specific button do what do you want.
//Then exit from loop by break.
break;
}
}
}

If you want to see what button was clicked then you can do the following once you create and assign the buttons. Considering that you create the button IDs manually:
protected void btn_click(object sender, EventArgs e) {
Button btn = (Button)sender // if you're sure that the sender is button,
// otherwise check if it is null
if(btn.ID == "blablabla")
// then do whatever you want
}
You can also check them from giving a command argument to each button.

Check out this example How to create 5 buttons and assign individual click events dynamically in C#

seems like this works, while adding a tag with each element of the array
Button button = sender as Button;
do you know of a better way?

In regards to your comment saying you'd like to know which button was clicked, you could set the .Tag attribute of a button to whatever kind of identifying string you want as it's created and use
private void MyButtonHandler(object sender, EventArgs e)
{
string buttonClicked = (sender as Button).Tag;
}

Related

many button in windows form with same event, in event i need name of button that selected

i have 46 button in form and all of them to same work just diffrent in value.
mean button1 plus 1 to sum , button2 plus 2 to sum ...
is there a way to understand which one of buttons are pressed to get it Text
private void button1_Click(object sender, EventArgs e)
{
// call a function with this button TEXt
// if button 1 selected -> func("1")
// if button 2 selected -> func("2")
}
is there any way do it ?
sender returns your button.So you can get the button like
Button clicked = (Button)sender;
In specifying the buttons, you can check that sender button's Text or you can give them Tag and check them. You said text, so
Button clicked = (Button)sender;
func(clicked?.Text);
should work.
Button clickedButton = sender as Button;
if (clickedButton != null)
{
button.Name....
}
The most straightforward solution is to create for each button an event_click (so button1_Click, button2_Click etc. Better to name the buttons appropriate.
Than from within each event handler, call the same function to add the number to the sum e.g.:
private void button1_Click(object sender, EventArgs e)
{
add(1);
}
private void button2_Click(object sender, EventArgs e)
{
add(2);
}
// Same for other event handlers.
private void add(int number)
{
sum += number;
}
You can link all your event handlers and then use the Name property to decide which button was clicked.
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Click += this.button1_Click;
private void button1_Click(object sender, EventArgs e)
{
var btn = sender as Button;
switch(btn.Name.ToLower())
{
case "button1":
MessageBox.Show("Add 1");
break;
case "button2":
MessageBox.Show("Add 2");
break;
default:
MessageBox.Show("Button not found");
break;
}
}

Adding a function to one button in an array of buttons

I am trying to make a calculator in C# through code only (Just adding a groupBox and a label in designer).
I have successfully added buttons a coma and a +/- sign.
But now I am running into a problem.
I can assign a function to all of them but
I need to assign a function separately to one of the buttons.
so far I have tried this with one button :
double num = Convert.ToDouble(Answer.Text);
Answer.Text = (-num).ToString();
But since I am working with arrays I have no idea how I can separate one button from another.
Essentially I should be able to do this:
If(Numbers[10].IsPressed)
do this.
but again I don't know the syntaxe's well enough
I dont really know what you want to do... But you might try a 'big' event handler... example:
Button[] buttons;
foreach(var button in buttons)
{
button.Click += MyHandler;
}
// method1
private void MyHandler(object sender, EventArgs e)
{
if(sender == buttons[0])
// Do something...
else if(sender == buttons[1])
// do something else...
else if(sender == buttons[2])
// and so on...
}
// method2
private void MyHandler(object sender, EventArgs e)
{
var button = (Button)sender;
switch(button.Text)
{
case "+":
case "1":
case "2":
// and so on...
}
}
Another good approach is having own handler for every button. Usualy Visual Studio will generate own click handler when you double click button in the designer.
private void ButtonPlus_Click(object sender, EventArgs e)
{
// Do something when "+" pressed
}
private void ButtonMinus_Click(object sender, EventArgs e)
{
// Do something when "-" pressed
}
You can manually add event handlers to the buttons in constructor (in case you used Visual Studio for generating it - Visual Studio will automatically add generated event handler to the button)
this.ButtonPlus.Click += ButtonPlus_Click;
this.ButtonMinus.Click += ButtonMinus_Click;
With approach above you will have own functions for every operations, which can help to maintain your code base little bid easily/faster.
However if you want to use only one event handler for all buttons, then you can use Button.Tag property.
Create method for every operation you have
private void ExecutePlus()
{
// Do something when "+" pressed
}
private void ExecuteMinus()
{
// Do something when "+" pressed
}
Then in constructor save all operations in the .Tag property of correspondent button.
this.ButtonPlus.Tag = ExecutePlus;
this.ButtonMinus.Tag = ExecuteMinus;
// add "general" event handler
var buttons = new[] { this.ButtonPlus, this.ButtonMinus };
foreach(var button in buttons)
{
button.Click += Button_Click;
}
Then create "general" click handler
private void Button_Click(object sender, EventArgs e)
{
var button = (Button)sender;
Action execute = (Action)button.Tag;
execute();
}
You will still have separated method for every operation, but only one event handler for all buttons

Set button handle in C#

btnName1 = new Button();
counter++;
//Start setting of Button
btnName1.Location = new Point(47, 35 + a);
btnName1.Size = new Size(132, 59);
btnName1.FlatStyle = FlatStyle.Popup;
btnName1.Text = textBox1.Text;
btnName1.Name = "btn" + counter.ToString();
btnName1.BackColor = Color.White;
btnName1.ForeColor = Color.Black;
panel1.Controls.Add(btnName1);
a += btnName1.Size.Height + 2;
btnName1.Click += BtnName1_Click;
I wrote this code for making a new button. When I click on add button this code runs and by each click on add button we can make new button.
But my problem is this
How can I set click handle for each button? I mean when I click on each button, they show their text to me
and I wrote this code to make the texts different:
btnName1.Text = textBox1.Text;
You didn't post your event code, but as Steve mentioned in his comment (that appears to have since been removed), you can use the sender argument to get the particular button that was clicked. Something like the following should be what you're after:
private void BtnName1_Click(object sender, EventArgs e)
{
//Access the text with: (sender as Button).Text
//Example: Write this button's text to the debug output window
Debug.WriteLine((sender as Button).Text);
}
Just be careful that in my specific example, you're only subscribing a Button to this event.
Prefered way is what you've put so far
btnName1.Click += BtnName1_Click;
...
private void BtnName1_Click(object sender, EventArgs e) {
// Button which has been clicked
Button button = sender as Button;
//TODO: put relevant code here
...
}
However, you can assign to each button its own event handler:
btnName1.Click += (s, e) => {
Button button = s as Button;
//TODO: put relevant code here
...
};

Dynamically create buttons and add events to it

I need to dynamically create buttons (one for loop) and add "onClick" and "doubuleClick" events on it.
I did it like this:
Button bt = new Button();
bt.Click += bt_Click;
bt.DoubleClick += bt_DoubleClick;
private void bt_Click(object sender, EventArgs e)
{
label1.Text = this.Text;
}
private void bt_DoubleClick(object sender, EventArgs e)
{
//some code
}
First: My "bt_Click" method gets "main form" text in "label1". In debugger I see that sender is a button. What is wrong with it?
Second: My "bt_DoubleClick" event do not react at all, am I doing something wrong here?
Any help is appreciated.
You should cast sender to Button to get the bt.Text:
Button bt = new Button();
bt.Click += bt_Click;
bt.Text = "click me";
bt.Location = new Point(100,100);
this.Controls.Add(bt);
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (sender as Button).Text;
}
Buttons doesn't react to double click event. You can read it here in detail.
In response to the first question, if I understand you correctly, in this.Text, this refers to the form because the method bt_Click is a member of the Main Form class. I think you might have meant to do:
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (Button)sender.Text;
}
Second: Is this just a case of the bt_Click handler firing twice?
The easiest way to do this it is to use "datagrid".
Datagread has the great support for all events and for organization of items (image, text and so on).
I have made "save" or "open" dialog form to browse content from remote SFTP server, very easy with datagrad, but I had a problem to do it with buttons or labels.

Event handler for groupBox with radioButtons in C#

I have some radionButtons in groupBox and I need to do action what I could call "one of radiobuttons.checked changed" or find out from radiobutton what index is changed.
I've tryed to find it in list of events but I couldn't find the right one.
Edit:
To make it more clear: I need to know if exist some handel for what I'll write handler method for the goupBox not for single radioButton. I know how to use radiButton.checkedChanged, but it's not what I'm finding ..
Or differently I need to know what options have the groupBox in monitoring what happens inside this groupBox - I mean only the handlers for the groupBox. I'm finding handler "in the group box is something happens" or simimilar if any exist.
It's in WFA (Windows Presentation Application) in Visual studio 2012.
I think what you want to do is wire up all of the RadioButtons' CheckedChanged event to the same handler.
public Form1()
{
radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
// ...
}
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
if (radioButton1.Checked)
{
// Do stuff
}
else if (radioButton2.Checked)
{
// Do other stuff
}
}
Nothing built in for that as far as I'm aware.
Set the tag property to some sort of indicator (0 to n) will do.
Add a CheckChangedHandler
Point all the buttons CheckChanged events at it.
then something like.
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
int buttonid = (int)radioButton.Tag;
switch (buttonid)
{
case 0 : // do something; break
}
}
If you've got a few of these I'd look at a radiogroup component.
I had the same problem: a group box named Button Type (gbxButtonType) with 6 radio buttons and another group box named Icon Type (gbxIconType) with 8 radio button. When the user selected one radio button from each group box, a MessageBox will appear with the selection applied after clicking the DisplayButton. My problem was that the group boxes didn't have a CheckedChanged event. The solution of AKN worked perfectly:
public Form1()
{
InitializeComponent();
for (int i = 0; i < gbxButtonType.Controls.Count; i++)
{
RadioButton rdb = (RadioButton)gbxButtonType.Controls[i];
rdb.CheckedChanged += new System.EventHandler(gbxButtonType_CheckedChanged);
}
for (int i = 0; i < gbxIconType.Controls.Count; i++)
{
RadioButton rdb = (RadioButton)gbxIconType.Controls[i];
rdb.CheckedChanged += new System.EventHandler(gbxIconType_CheckedChanged);
}
}
private void gbxIconType_CheckedChanged(object sender, EventArgs e)
{
if (sender == rdbAsterisk)
{
iconType = MessageBoxIcon.Asterisk;
}
else if (sender == rdbError)
{
iconType = MessageBoxIcon.Error;
}
...
else
{
iconType = MessageBoxIcon.Warning;
}
}
Similar to davenewza's answer (and likely should have been a comment, but I have insufficient reputation), but with the event firing only once for the entire group of radio buttons.
public Form1()
{
// Add a "CheckedChanged" event handler for each radio button.
// Ensure that all radio buttons are in the same groupbox control.
radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
}
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
// Do stuff only if the radio button is checked (or the action will run twice).
if (((RadioButton)sender).Checked)
{
if (((RadioButton)sender) == radioButton1)
{
// Do stuff
}
else if (((RadioButton)sender) == radioButton2)
{
// Do other stuff
}
}
}
Groupbox will limit only one radio button checked
So Setp1: you can assign one "CheckedChanged" event handler to all you radio button
private void initRadio()
{
radio_button1.CheckedChanged += Radio_show_CheckedChanged;
radio_button2.CheckedChanged +=Radio_show_CheckedChanged;
}
And Setp2: implement this event handler like this (Filter by Radio Button's Text)
private void Radio_show_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
if (radioButton.Checked == true) { //limited only checked button do function
switch (radioButton.Text)
{
case "name1":
// do your stuff ...
break;
case "name2":
// do your stuff ...
break;
}
}
}
System.Windows.Forms.RadioButton.CheckedChanged
is the event you need
So do something like:
public Form1()
{
InitializeComponent();
this.radioButton1.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// your action
}
I think your want to handle the selection of some radio buttons inside a groupbox using the groupbox control itself.
May be you wanted this basically to avoid code repetition.
(i.e) adding check change event for all the radio button in the designer which may be tedious when there are more control.
Since its already present under a group, why not use the group control object to manipulate controls with-in it and set the events.
This is how I understood your problem and hence the solution as indicated below.
Set a common handler for all radio button control in the group box
for (int i = 0; i < groupBox.Controls.Count; i++)
{
RadioButton rb = (RadioButton)groupBox.Controls[i];
rb.CheckedChanged += new System.EventHandler(evntHandler);
}
Inside the handler, you can determine which button was changed as indicated by others and do the necessary action.
//Here you go courtesy of Jock Frank Halliday
//^subscribe events to radio button check changed
private void seriesTxtBxEvent()
{
//Show txtBx
this.radBtn_RoomSeries.CheckedChanged += new EventHandler(showSeriesTxtBx_Event);
//Hide txtBx
this.radBtn_RoomNumber.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
this.radBtn_RoomName.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
this.radBtn_RoomLevel.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
this.radBtn_RoomDep.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
}
private void hideSeriesTxtBx_Event(object sender, EventArgs e)
{
tbx_SheetSeries.Visible = false;
}
private void showSeriesTxtBx_Event(object sender, EventArgs e)
{
tbx_SheetSeries.Visible = true;
}
//Form Start
void MainFormLoad(object sender, EventArgs e)
{
Control.ControlCollection locais = groupBoxLocalização.Controls;
foreach (CheckBox chkBox in locais)
{
chkBox.MouseUp += chkBoxLocais_MouseUp;
}
}
// Event
void chkBoxLocais_MouseUp(object sender, MouseEventArgs e)
{
//Tratar individualmente
CheckBox chk = (CheckBox)sender;
//ou para tratar todos objetos de uma vez
Control.ControlCollection locais = groupBoxLocalização.Controls;
foreach (CheckBox chkBox in locais) {
//chkBox....
}
}
You can maybe do it with Timer, but that's just bad for optimalization, the easy solution is that for every radiobutton you simply add only one function as ChekedChanged event.
Create a Checked event by double clicking on any of the radio buttons, copy the name of the method that Visual Studio creates for the event
Go to all radio buttons and change the event to the copied one from Properties Explorer > Events Section
In the generated method use the following code. This would fire event for all radio buttons but only "Do your thing" once
Code:
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked == false) return;
// Do your thing
}
Create Event Checked_Changed on one radio button from Designer Events list.
Add same event to each radio Button from dropdown in front of Checked_Changed
event of each radio
inside checked changed event use
private void CheckedChanged(object sender,EventArgs e)
{
var radio = groupBox.Controls.OfType<RadioButton>
().FirstOrDefault(r => r.Checked).Name;
}
you can get which radio is active now.

Categories

Resources