locked
how to do somthing for multible button in runtime RRS feed

  • Question

  • i have a form with 500 buttons when i click in one button it change his bakground to blue (" it is a workaround to know this buton is selected") so he can check many buttons .

    how can i get a list with name of this buttons who is checked to change the text of this buttons?

    any help please in this?


    Thanks for any help :) my blog is: http://www.waelk.com

    Tuesday, October 30, 2012 8:48 AM

Answers

  • Is this you after?

     private void Form1_Load(object sender, EventArgs e)
     {
                button1.Click += new EventHandler(Button_Click);
                button2.Click += new EventHandler(Button_Click);
     }
    void Button_Click(object sender, EventArgs e)
     {
                if (sender == button1)
                    MessageBox.Show("Button1");
                if (sender == button2)
                    MessageBox.Show("Button2");
     }


    If a post answers your question, please click "Mark As Answer" and "Mark as Helpful" on that post.

    • Proposed as answer by Jason Dot Wang Thursday, November 1, 2012 6:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 7, 2012 8:54 AM
    Tuesday, October 30, 2012 9:08 AM
  • Hi,

    Add an extension method to put all controls in one collection and you can query it using LINQ. This gives you great possibilities to get for instance all buttons with a specific background color.

    An example of this can be found on the blog of Andrew Pallant, in the article Using LINQ to find a control in the ControlCollection.

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    • Proposed as answer by Jason Dot Wang Thursday, November 1, 2012 6:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 7, 2012 8:54 AM
    Tuesday, October 30, 2012 9:12 AM
  • Your workaround imho makes it unnecessarily complex. Use a CheckBox, see this ToggleButton thread.
    • Proposed as answer by Jason Dot Wang Thursday, November 1, 2012 6:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 7, 2012 8:54 AM
    Tuesday, October 30, 2012 9:13 AM

All replies

  • Is this you after?

     private void Form1_Load(object sender, EventArgs e)
     {
                button1.Click += new EventHandler(Button_Click);
                button2.Click += new EventHandler(Button_Click);
     }
    void Button_Click(object sender, EventArgs e)
     {
                if (sender == button1)
                    MessageBox.Show("Button1");
                if (sender == button2)
                    MessageBox.Show("Button2");
     }


    If a post answers your question, please click "Mark As Answer" and "Mark as Helpful" on that post.

    • Proposed as answer by Jason Dot Wang Thursday, November 1, 2012 6:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 7, 2012 8:54 AM
    Tuesday, October 30, 2012 9:08 AM
  • Hi,

    Add an extension method to put all controls in one collection and you can query it using LINQ. This gives you great possibilities to get for instance all buttons with a specific background color.

    An example of this can be found on the blog of Andrew Pallant, in the article Using LINQ to find a control in the ControlCollection.

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    • Proposed as answer by Jason Dot Wang Thursday, November 1, 2012 6:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 7, 2012 8:54 AM
    Tuesday, October 30, 2012 9:12 AM
  • Your workaround imho makes it unnecessarily complex. Use a CheckBox, see this ToggleButton thread.
    • Proposed as answer by Jason Dot Wang Thursday, November 1, 2012 6:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, November 7, 2012 8:54 AM
    Tuesday, October 30, 2012 9:13 AM