-
October 28th, 2005, 02:59 AM
#1
Event handling for a 144 buttons!
I've reached the next stage of my first real c# project thanks to some folks on here. I now need to implement button click events for 144 buttons!
Each time any of the buttons are clicked a check must be done (a background calculation) and then the properties for that button need to be set depending on the results of that calculation.
E.g.
Code:
private void button144_Click(object sender, System.EventArgs e)
{
int x = someFunction();
if (x>5)
{
button144.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
button144.BackColor = System.Drawing.Color.DarkGray;
}
}
I could simply write out the code for all 144 buttons but that would be far to boring !!!
Can anyone help me?
-
October 28th, 2005, 04:34 AM
#2
Re: Event handling for a 144 buttons!
I think that you create the buttons dynamically and add event handler like this:
Code:
private void Form1_Load(object sender, System.EventArgs e)
{
Button b=null;
for(int n=0;n<144;n++)
{
b=new Button();
this.Controls.Add(b);
//position the controls
b.Click+=new EventHandler(button144_Click);
}
}
private void button144_Click(object sender, System.EventArgs e)
{
Button b=(Button)sender;
MessageBox.Show(b.Name);
//do actions on b;
}
-
October 30th, 2005, 02:22 PM
#3
Re: Event handling for a 144 buttons!
You dont need to create them at runtime.
In your Form_Load function...
Code:
foreach (Control c in Controls)
{
if (typeof(c) == typeof(Button))
c.Click += new EventHandler(all_buttons_click)
}
Last edited by Daniel1324; October 30th, 2005 at 09:38 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|