Home All Groups Group Topic Archive Search About

Multiple Events in a single method

Author
4 Jan 2005 1:41 PM
Forch
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
Author
4 Jan 2005 1:50 PM
Herfried K. Wagner [MVP]
"Forch" <Fo***@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/>
Are all your drivers up to date? click for free checkup

Author
4 Jan 2005 1:50 PM
Carlos J. Quintero [.NET MVP]
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


Show quoteHide quote
"Forch" <Fo***@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
Author
4 Jan 2005 3:03 PM
Forch
Thanks Carlos!  That is exactly what I am looking for.

Cheers,

Forch

Show quoteHide quote
"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" <Fo***@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
>
>
>
Author
4 Jan 2005 3:39 PM
carlmanaster
Is there syntax available to do this from C# as well?  Thanks.
Author
4 Jan 2005 4:03 PM
Carlos J. Quintero [.NET MVP]
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


Show quoteHide quote
"carlmanaster" <carl.manas***@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.
>
Author
4 Jan 2005 5:05 PM
carlmanaster
Cool!  Thanks!

Bookmark and Share