PC Review


Reply
Thread Tools Rate Thread

Dynamic controls and handlers

 
 
David D Webb
Guest
Posts: n/a
 
      23rd Aug 2004
I have a form with a number of buttons that are generated in a panel based
on the results of a query. They all use the same click event handler. I am
having trouble determining which button was pressed. I can only go by the
button text or the button position to distinguish them - neither of which
works well for my situation. Is there a better way? Should I create my own
button class inheiriting Button and add my own custom id fields?

Thanks/Dave

They are created as:

while (...)
{
Button btnClient = new Button();
btnClient.Location = new System.Drawing.Point(8, (numClients*32) + 8);
btnClient.Width = 150;
btnClient.Text = full_name;
btnClient.Click += new System.EventHandler(btnClient_Click);
pnlScroll.Controls.Add(btnClient);
numClients ++;
}

I have a generic handler for these buttons:

private void btnClient_Click(object sender, System.EventArgs e)
{
Button btnClient = (Button)sender;
// Determine which button was pressed here

}


 
Reply With Quote
 
 
 
 
Peter Foot [MVP]
Guest
Posts: n/a
 
      23rd Aug 2004
There are a number of options, here are a few:-

You could add references to your buttons to a hashtable with a unique value
to identify them, and then do a lookup on this from the sender argument of
your event handler.

You could use Chris's code sample for determining the control name at
runtime:-
http://blog.opennetcf.org/ctacke/Per...2de4bdaf1.aspx

Finally you could derive your own control from Button and add a Name, Tag or
similar property which you can use to uniquely identify each one. However
there will be a bit of extra work required to create designer support for
the control.

Peter

--
Peter Foot
Windows Embedded MVP
www.inthehand.com | www.opennetcf.org

Do have an opinion on the effectiveness of Microsoft Windows Mobile and
Embedded newsgroups? Let us know!
https://www.windowsembeddedeval.com/...ity/newsgroups

"David D Webb" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a form with a number of buttons that are generated in a panel based
>on the results of a query. They all use the same click event handler. I am
>having trouble determining which button was pressed. I can only go by the
>button text or the button position to distinguish them - neither of which
>works well for my situation. Is there a better way? Should I create my
>own button class inheiriting Button and add my own custom id fields?
>
> Thanks/Dave
>
> They are created as:
>
> while (...)
> {
> Button btnClient = new Button();
> btnClient.Location = new System.Drawing.Point(8, (numClients*32) + 8);
> btnClient.Width = 150;
> btnClient.Text = full_name;
> btnClient.Click += new System.EventHandler(btnClient_Click);
> pnlScroll.Controls.Add(btnClient);
> numClients ++;
> }
>
> I have a generic handler for these buttons:
>
> private void btnClient_Click(object sender, System.EventArgs e)
> {
> Button btnClient = (Button)sender;
> // Determine which button was pressed here
>
> }
>



 
Reply With Quote
 
David D Webb
Guest
Posts: n/a
 
      23rd Aug 2004
Thanks Peter,

Since they were dynamic controls, I didn't need designer support, so the
third solution turned out to be extremely easy. Love this .NET stuff...

-Dave

"Peter Foot [MVP]" <(E-Mail Removed)> wrote in message
news:%23DnkD%(E-Mail Removed)...
> There are a number of options, here are a few:-
>
> You could add references to your buttons to a hashtable with a unique
> value to identify them, and then do a lookup on this from the sender
> argument of your event handler.
>
> You could use Chris's code sample for determining the control name at
> runtime:-
> http://blog.opennetcf.org/ctacke/Per...2de4bdaf1.aspx
>
> Finally you could derive your own control from Button and add a Name, Tag
> or similar property which you can use to uniquely identify each one.
> However there will be a bit of extra work required to create designer
> support for the control.
>
> Peter
>
> --
> Peter Foot
> Windows Embedded MVP
> www.inthehand.com | www.opennetcf.org
>
> Do have an opinion on the effectiveness of Microsoft Windows Mobile and
> Embedded newsgroups? Let us know!
> https://www.windowsembeddedeval.com/...ity/newsgroups
>
> "David D Webb" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I have a form with a number of buttons that are generated in a panel based
>>on the results of a query. They all use the same click event handler. I
>>am having trouble determining which button was pressed. I can only go by
>>the button text or the button position to distinguish them - neither of
>>which works well for my situation. Is there a better way? Should I
>>create my own button class inheiriting Button and add my own custom id
>>fields?
>>
>> Thanks/Dave
>>
>> They are created as:
>>
>> while (...)
>> {
>> Button btnClient = new Button();
>> btnClient.Location = new System.Drawing.Point(8, (numClients*32) + 8);
>> btnClient.Width = 150;
>> btnClient.Text = full_name;
>> btnClient.Click += new System.EventHandler(btnClient_Click);
>> pnlScroll.Controls.Add(btnClient);
>> numClients ++;
>> }
>>
>> I have a generic handler for these buttons:
>>
>> private void btnClient_Click(object sender, System.EventArgs e)
>> {
>> Button btnClient = (Button)sender;
>> // Determine which button was pressed here
>>
>> }
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic event handlers eaguilar Microsoft Dot NET Framework Forms 1 21st Feb 2008 12:18 PM
Problem with Event handlers for Dynamic created Controls in DatGri =?Utf-8?B?U2hpanUgUG95aWxpbA==?= Microsoft ASP .NET 2 10th May 2005 02:17 PM
Help: Is it possible that remove event handlers dynamic? Henry Jia Microsoft Dot NET Framework 2 11th Nov 2004 04:03 AM
Dynamic event handlers John Microsoft C# .NET 1 17th Mar 2004 12:40 AM
Handlers and dynamic buttons noone Microsoft ASP .NET 1 27th Jan 2004 11:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:16 PM.