Assigning The Same Event To All Buttons In A Panel?

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 adn258  Icon User is offline

  • D.I.C Addict

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

Assigning The Same Event To All Buttons In A Panel?

Posted 29 September 2013 - 06:05 PM

So lets pretend we need a very simple event for buttons where they are all numbers and you want to add the number of the button to a label or textbox when the button is clicked (that is all). Would it be acceptable to do something like this:

InitializeComponent();
 foreach (Control c in this.flwPnlNumberBtns.Controls)
            {
                if (c is Button)
                {
                    c.Click += (o, e) =>
                        {
                            this.lblCalculations.Text += c.Text;
                        };
                }
            }



So for example in a calculator you have buttons and their text is simply a number and you could add that to a label (for example).

Is there anything wrong with doing this? Also does this create any performance downgrade using the foreach loop Vs's just assigning an individual event to each Number button?

Is there anything more efficient? Elaborate? Thanks guys

Is This A Good Question/Topic? 0
  • +

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

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

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

Posted 29 September 2013 - 06:12 PM

Its likely not pointing at the same lambda
Was This Post Helpful? 0
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

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

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

Posted 29 September 2013 - 06:20 PM

View PostAdamSpeight2008, on 29 September 2013 - 06:12 PM, said:

Its likely not pointing at the same lambda


What do you mean friend and what does that have to do with anything? Are you saying that I should use one handler for all of these? That could be right. Elaborate friend?
Was This Post Helpful? 0
  • +
  • -

#4 adn258  Icon User is offline

  • D.I.C Addict

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

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

Posted 29 September 2013 - 06:25 PM

Alternatively I could do this:



 foreach (Control c in this.flwPnlNumberBtns.Controls)
            {
                if (c is Button)
                {
                    c.Click += new EventHandler(numbersClicked);
                }
            }
        }

        protected void numbersClicked(object sender, EventArgs e)
        {
            this.lblCalculations.Text += (sender as Button).Text;
        }



Is there anything wrong with doing this? Now you're using one handler and adding it to each button
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

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

Posted 29 September 2013 - 06:49 PM

In vb.net, it lets you write this.
Private Sub CheckBoxes_CheckedChanged( sender As Object,  e As EventArgs) Handles Salary.CheckedChanged, 
  Age.CheckedChanged, Years.CheckedChanged, CreditRating.ValueChanged, Disaster.CheckedChanged, Bankrupt.CheckedChanged
    QualifyText
End Sub 


All those events are handled by the same handler.

Can't find any "wiring up" in the designer.vb

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;
                }
            }


This post has been edited by AdamSpeight2008: 29 September 2013 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 adn258  Icon User is offline

  • D.I.C Addict

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

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

Posted 29 September 2013 - 09:02 PM

Adam how is that any different essentially than what I did above? Also what are the performance implications of your method (if any)?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,467
  • Joined: 05-May 12

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

Posted 29 September 2013 - 09:07 PM

In your post #1, you have no guarantees that your lambda expression would result in multiple instances, or a single one as the loop iterates. With Adam's version, he knows that he's only got one instance. Since you were asking that all events go to the same event handler, Adam's version fulfills your request.
Was This Post Helpful? 0
  • +
  • -

#8 adn258  Icon User is offline

  • D.I.C Addict

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

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

Posted 29 September 2013 - 09:10 PM

Thanks skydiver. How about in my second post that guarantees it will use that handler correct? Also btw as best as I know you can't use VAR for holding reference to a lambda expression like that in Adam's code you would need to use EventHandler like so

EventHandler numHandler = (object sender, EventArgs e) =>
                {

                };



ALSO ONE THING I MUST CONFESS what is the difference (if any) of using Adam's example pretending he used EVentHandler of using

c.Click += x; 


VS

c.Click += new EventHandler(x); 

This post has been edited by adn258: 29 September 2013 - 09:15 PM

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,467
  • Joined: 05-May 12

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

Posted 29 September 2013 - 09:58 PM

In your second post, you are calling new. Were you expecting the compiler to ignore your requests to create a new EventHandler and just keeping giving you a copy of the first one it gave you?
Was This Post Helpful? 0
  • +
  • -

#10 adn258  Icon User is offline

  • D.I.C Addict

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

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

Posted 29 September 2013 - 10:38 PM

Thanks skydiver but why is that bad. Also what if I didn't call new and I just used the

c.Click += EventHandlerHere;



I'm assuming that it's not actually creating a new eventhandler when you just use += but instead assigning the same handler that already exists. This can be confusing
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,467
  • Joined: 05-May 12

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

Posted 29 September 2013 - 11:10 PM

From http://msdn.microsof.../ms366768.aspx:

Quote

publisher.RaiseCustomEvent += HandleCustomEvent;


Note that the previous syntax is new in C# 2.0. It is exactly equivalent to the C# 1.0 syntax in which the encapsulating delegate must be explicitly created by using the new keyword:
publisher.RaiseCustomEvent += new CustomEventHandler(HandleCustomEvent);


Was This Post Helpful? 0
  • +
  • -

#12 adn258  Icon User is offline

  • D.I.C Addict

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

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

Posted 29 September 2013 - 11:34 PM

Thanks SkyDiver so essentially as it stands now they are basically the same thing. I'm going with the method by assigning the event delegate to a variable and assigning it as the one to use with +=.

It's my guess that technically this makes almost no performance difference VS specifying each button event individually instead of the code in the foreach loop
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

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

Posted 30 September 2013 - 08:07 AM

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>.
Was This Post Helpful? 1
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

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

Posted 30 September 2013 - 10:19 AM

Lambdas are a pain point for inference, most people will be wanting Action<object,EventArgs> than expression< Action<object,EventArgs> >.
So why not default to that type in the compiler? If really wanted the expression then use expression.
Was This Post Helpful? 1
  • +
  • -

#15 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

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

Posted 30 September 2013 - 10:55 AM

Agreed. I hope they make that change in the future.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2