Hello everyone...

I have 4 buttons (Button_1, Button_2, etc...), and I want to call the same
method regardless of which button is being clicked.

Once I am in the event, how do I know which button was pressed? Or do I
have to call a separte method for each button? Or is there magic with
"Sender as system.object" ?

Thanks,

Forch

Re: Multiple Events in a single method by Herfried

Herfried
Tue Jan 04 07:50:31 CST 2005

"Forch" <Forch@discussions.microsoft.com> schrieb:
> I have 4 buttons (Button_1, Button_2, etc...), and I want to call the same
> method regardless of which button is being clicked.
>
> Once I am in the event, how do I know which button was pressed? Or do I
> have to call a separte method for each button? Or is there magic with
> "Sender as system.object" ?

\\\
Private Sub Button_Click(...) Handles Button1.Click, Button2.Click
Dim SourceControl As Button = DirectCast(sender, Button)
SourceControl.Text = "I was clicked!"
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



Re: Multiple Events in a single method by Carlos

Carlos
Tue Jan 04 07:50:37 CST 2005

Yes, you have to use the Sender parameter:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click

If sender Is Button1 Then
Me.Text = "You clicked Button1"
ElseIf sender Is Button2 Then
Me.Text = "You clicked Button2"
End If

End Sub
--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com


"Forch" <Forch@discussions.microsoft.com> escribió en el mensaje
news:8557EED3-A42B-40BD-A33E-8435BAFF9D73@microsoft.com...
> Hello everyone...
>
> I have 4 buttons (Button_1, Button_2, etc...), and I want to call the same
> method regardless of which button is being clicked.
>
> Once I am in the event, how do I know which button was pressed? Or do I
> have to call a separte method for each button? Or is there magic with
> "Sender as system.object" ?
>
> Thanks,
>
> Forch



Re: Multiple Events in a single method by Forch

Forch
Tue Jan 04 09:03:08 CST 2005

Thanks Carlos! That is exactly what I am looking for.

Cheers,

Forch

"Carlos J. Quintero [.NET MVP]" wrote:

> Yes, you have to use the Sender parameter:
>
> Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click, Button2.Click
>
> If sender Is Button1 Then
> Me.Text = "You clicked Button1"
> ElseIf sender Is Button2 Then
> Me.Text = "You clicked Button2"
> End If
>
> End Sub
> --
>
> Carlos J. Quintero
>
> MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
> You can code, design and document much faster.
> http://www.mztools.com
>
>
> "Forch" <Forch@discussions.microsoft.com> escribió en el mensaje
> news:8557EED3-A42B-40BD-A33E-8435BAFF9D73@microsoft.com...
> > Hello everyone...
> >
> > I have 4 buttons (Button_1, Button_2, etc...), and I want to call the same
> > method regardless of which button is being clicked.
> >
> > Once I am in the event, how do I know which button was pressed? Or do I
> > have to call a separte method for each button? Or is there magic with
> > "Sender as system.object" ?
> >
> > Thanks,
> >
> > Forch
>
>
>

Re: Multiple Events in a single method by carlmanaster

carlmanaster
Tue Jan 04 09:39:28 CST 2005

Is there syntax available to do this from C# as well? Thanks.


Re: Multiple Events in a single method by Carlos

Carlos
Tue Jan 04 10:03:07 CST 2005

Yep:

this.button1.Click += new System.EventHandler(this.button_Click);
this.button2.Click += new System.EventHandler(this.button_Click);

private void button_Click(object sender, System.EventArgs e)
{
if (sender == this.button1)
this.Text = "You clicked button 1";
else if (sender == this.button2)
this.Text = "You clicked button 2";

}

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com


"carlmanaster" <carl.manaster@gmail.com> escribió en el mensaje
news:1104853168.372067.32720@c13g2000cwb.googlegroups.com...
> Is there syntax available to do this from C# as well? Thanks.
>



Re: Multiple Events in a single method by carlmanaster

carlmanaster
Tue Jan 04 11:05:05 CST 2005

Cool! Thanks!