Selecting buttons
-
I'm working on a little project for school and I'm having a little problem with selecting a button. I have some buttons and I have to detect which 2 buttons I just clicked so I can compare the values in it. How do I do that? I can compare the two values, so that's not the problem, I want to know how to select two unknown buttons. My code looks a bit like this
//the text in my buttons is a number! int comp1; int comp2; Button btncompare1; Button btncompare2; //Here's the problem! this is the only way I can select buttons //but I have to select them by a click. btncompare1=button1; btncompare2=button2; comp1=Convert.ToInt32(btncompare1.Text); comp2=Convert.ToInt32(btncompare2.Text); if(comp1>comp2) ...
I hope you understand my problem!!! Thanks -
I'm working on a little project for school and I'm having a little problem with selecting a button. I have some buttons and I have to detect which 2 buttons I just clicked so I can compare the values in it. How do I do that? I can compare the two values, so that's not the problem, I want to know how to select two unknown buttons. My code looks a bit like this
//the text in my buttons is a number! int comp1; int comp2; Button btncompare1; Button btncompare2; //Here's the problem! this is the only way I can select buttons //but I have to select them by a click. btncompare1=button1; btncompare2=button2; comp1=Convert.ToInt32(btncompare1.Text); comp2=Convert.ToInt32(btncompare2.Text); if(comp1>comp2) ...
I hope you understand my problem!!! ThanksWhat about using two global variables (or an array, even better), and a counter. Then, when one button is clicked, the reference to the instance is saved in the first position, and when another button is clicked, it is saved in the second. Like this:
public class MyClass
{
// Initialize a new array with a length of two (thus index 0 and 1):
private Button[] _buttons = new Button[2];
private int _counter = 0;// Handle the events of your buttons.
public MyClass()
{
MyButton1.Click += new EventHandler(Button_Click);
MyButton2.Click += new EventHandler(Button_Click);
MyButton3.Click += new EventHandler(Button_Click);
// Etc...
}private void Button_Click(object sender, EventArgs e)
{
// Assign your button to the array,
_buttons[_counter] = (Button)sender;
// and increase the counter by 1.
_counter++;
}
}Instead of manually assigning the events to your buttons in the class' constructor, you may want to use the Form-designer to do that. Just copy-and-paste the name of the method that handles the event in the field next to 'Click' on the 'Events' tab of the propertygrid of your buttons. Don't forget to reset the counter (
_counter = 0
) when you're finished, or add a check to see whether_counter > _buttons.Length - 1
, then:if (_counter > _buttons.Length - 1) _counter = 0;
-
What about using two global variables (or an array, even better), and a counter. Then, when one button is clicked, the reference to the instance is saved in the first position, and when another button is clicked, it is saved in the second. Like this:
public class MyClass
{
// Initialize a new array with a length of two (thus index 0 and 1):
private Button[] _buttons = new Button[2];
private int _counter = 0;// Handle the events of your buttons.
public MyClass()
{
MyButton1.Click += new EventHandler(Button_Click);
MyButton2.Click += new EventHandler(Button_Click);
MyButton3.Click += new EventHandler(Button_Click);
// Etc...
}private void Button_Click(object sender, EventArgs e)
{
// Assign your button to the array,
_buttons[_counter] = (Button)sender;
// and increase the counter by 1.
_counter++;
}
}Instead of manually assigning the events to your buttons in the class' constructor, you may want to use the Form-designer to do that. Just copy-and-paste the name of the method that handles the event in the field next to 'Click' on the 'Events' tab of the propertygrid of your buttons. Don't forget to reset the counter (
_counter = 0
) when you're finished, or add a check to see whether_counter > _buttons.Length - 1
, then:if (_counter > _buttons.Length - 1) _counter = 0;
Ok thanks it works now!:)