I'm a newbie the Managed VC++, after 11+ years of Windows and MFC. so bare
with me..
I have a WinForm that has 6 Buttons that have a lot of commonality between
them. When Clicked, I would like the same Event Handler Function to handle
all 6 Buttons and use a switch statement to handle to specifics for a given
Button. To me this seems like much less code having 6 Event Handler
Functions.
The second part of the issue is, in the Event Handler Function, how do I do
the equivalent of casting the Syetem::Object *s to Button *b so I can get at
the Button's (or any sender's) variables.
Thanks
JEK
> Hi John,
>
[quoted text clipped - 27 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Jochen Kalmbach [MVP] - 07 May 2005 12:02 GMT
Hi John!
> I have a WinForm that has 6 Buttons that have a lot of commonality between
> them. When Clicked, I would like the same Event Handler Function to handle
[quoted text clipped - 5 lines]
> the equivalent of casting the Syetem::Object *s to Button *b so I can get at
> the Button's (or any sender's) variables.
The easiest approch is to use one Event handler with using the
sender-object to determine the different buttons:
<code>
private: System::Void buttonX_Click(
System::Object * sender,
System::EventArgs * e)
{
Button *btn = dynamic_cast<Button*>(sender);
if (btn == button1)
{
MessageBox::Show(this, S"Button1");
}
else if (btn == button2)
{
MessageBox::Show(this, S"Button2");
}
else if (btn == button3)
{
MessageBox::Show(this, S"Button3");
}
else if (btn == button4)
{
MessageBox::Show(this, S"Button4");
}
}
</code>

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Yan-Hong Huang[MSFT] - 11 May 2005 03:16 GMT
Hi John,
I was reviewing the issue thread. Jochen has replied with code samples on
it. Is there any question on it? If yes, please feel free to post here and
we are glad to work with you.
Thanks very much for participating the community.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn
This posting is provided "AS IS" with no warranties, and confers no rights.