CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2005
    Posts
    16

    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?

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    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;
    			
    		}
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Oct 2003
    Posts
    66

    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
  •  





Click Here to Expand Forum to Full Width

Featured