|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Multiple Events in a single methodHello 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 "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/> 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 -- Show quoteHide quoteCarlos 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 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 > > > 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"; } -- Show quoteHide quoteCarlos 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.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. >
Other interesting topics
Clicking a button and pressing Enter key act different in MDI clie
OT: Replacement for Adobe Acrobat Adding/Updating assemblies in GAC during runtime and not during Se wiring events Drag Drop Data pictures to my Form New Thread Drag and Drop Message from outlook datagrid memory leak? Obtain default curtom control size Outlook Style Calendar Control |
|||||||||||||||||||||||