Assigning The Same Event To All Buttons In A Panel?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 527 Views - Last Post: 01 October 2013 - 05:49 PM Rate Topic: -----

#16 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Assigning The Same Event To All Buttons In A Panel?

Posted 01 October 2013 - 03:03 PM

View PostCurtis Rutland, on 30 September 2013 - 08:07 AM, said:

View PostAdamSpeight2008, on 29 September 2013 - 08:49 PM, said:

The following I know will point at the same lambda
InitializeComponent();
var x =(o, e) =>
                        {
                            this.lblCalculations.Text += c.Text;
                        };
 foreach (Control c in this.flwPnlNumberBtns.Controls)
            {
                if (c is Button)
                {
                    c.Click += x;
                }
            }



Just a minor correction, C# doesn't let you use var for Labmdas. So you'd have to replace it with Action<object, EventArgs>.


Very true friend. If you look above I used EventHandler because in this care there are parms that I need based on the sending object like so

 InitializeComponent();
            EventHandler numHandler = (o, e) =>
                {
                    addDigit((o as Button).Text);
                };
            foreach (Button b in this.flwPnlNumberBtns.Controls)
            {
                b.Click += numHandler;
            }




This post has been edited by adn258: 01 October 2013 - 03:05 PM

Was This Post Helpful? 0
  • +
  • -

#17 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Assigning The Same Event To All Buttons In A Panel?

Posted 01 October 2013 - 03:15 PM

Mind if I ask why you're using a lambda for this? I love lambdas, but in cases like this I think a named method is more appropriate. Lambdas add a layer of complexity that isn't always desirable, especially when you're working with a team where not all members are as proficient.

This would suffice just as well:

public Form1()
{
    InitializeComponent();
    foreach (var b in Controls.OfType<Button>())
        b.Click += CalculatorButtonclick;
}

public void CalculatorButtonclick(object sender, EventArgs e)
{
    var b = sender as Button;
    if (b == null) 
        throw new ArgumentException("sender was not button", "sender");
    MessageBox.Show("Clicked: " + b.Text);
}



With the added benefit of not having to understand lambdas.

I usually don't use them for event handlers unless they're very simple, like

Load += (sender, args) => SomeInitializationMethod();


Beyond that, I use named methods for handlers.
Was This Post Helpful? 0
  • +
  • -

#18 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Assigning The Same Event To All Buttons In A Panel?

Posted 01 October 2013 - 05:49 PM

Curtis that's essentially the code I had originally only instead of just using += I used

+= new EventHandler(methodhere)



Which doesn't appear to make any difference so maybe that WAS better. Both solutions are acceptable
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2