How can I detect which button is clicked on the form?
-
Hello! In shown sample below, I have a loop what ceates instances of a button. What I want to know is which one gets clicked in runtime? Thank you.
Public Sub AddButtons() For I = 1 to 10 Dim myButton as new Button myButton.text = "Hello" me.Controls.Add(myButton) AddHandler myButton.click, AddressOf myButtonEvent Next End Sub ' Here is the event handler for the button Private sub myButtonEvent (Byval Sender as Object, e as EventArg) 'Here I want to know which button has been clicked because there are 10 'buttons on the form. End Sub
What a curious mind needs to discover knowledge is noting else than a pin-hole.
-
Hello! In shown sample below, I have a loop what ceates instances of a button. What I want to know is which one gets clicked in runtime? Thank you.
Public Sub AddButtons() For I = 1 to 10 Dim myButton as new Button myButton.text = "Hello" me.Controls.Add(myButton) AddHandler myButton.click, AddressOf myButtonEvent Next End Sub ' Here is the event handler for the button Private sub myButtonEvent (Byval Sender as Object, e as EventArg) 'Here I want to know which button has been clicked because there are 10 'buttons on the form. End Sub
What a curious mind needs to discover knowledge is noting else than a pin-hole.
in your button event:
dim b as Button b= Ctype(sender,Button)
the b will be the button that was clicked. If you are wanting to run different code depending on what button was clicked you can check for the name, or assign a value to the tag property and use that for identification. Hope that helps:) -- modified at 11:34 Tuesday 13th March, 2007 -
in your button event:
dim b as Button b= Ctype(sender,Button)
the b will be the button that was clicked. If you are wanting to run different code depending on what button was clicked you can check for the name, or assign a value to the tag property and use that for identification. Hope that helps:) -- modified at 11:34 Tuesday 13th March, 2007 -
Hello! In shown sample below, I have a loop what ceates instances of a button. What I want to know is which one gets clicked in runtime? Thank you.
Public Sub AddButtons() For I = 1 to 10 Dim myButton as new Button myButton.text = "Hello" me.Controls.Add(myButton) AddHandler myButton.click, AddressOf myButtonEvent Next End Sub ' Here is the event handler for the button Private sub myButtonEvent (Byval Sender as Object, e as EventArg) 'Here I want to know which button has been clicked because there are 10 'buttons on the form. End Sub
What a curious mind needs to discover knowledge is noting else than a pin-hole.
You would want to set the button id value. Something like myButton.Id = "btn"+I.ToString(); Then in the event CType(Sender, Button).Id to figure out which button. Hope that helps. Ben
-
Hello! In shown sample below, I have a loop what ceates instances of a button. What I want to know is which one gets clicked in runtime? Thank you.
Public Sub AddButtons() For I = 1 to 10 Dim myButton as new Button myButton.text = "Hello" me.Controls.Add(myButton) AddHandler myButton.click, AddressOf myButtonEvent Next End Sub ' Here is the event handler for the button Private sub myButtonEvent (Byval Sender as Object, e as EventArg) 'Here I want to know which button has been clicked because there are 10 'buttons on the form. End Sub
What a curious mind needs to discover knowledge is noting else than a pin-hole.
Private Sub AddButtons Dim btn As Button For i As Integer = 1 To 10 btn = New Button() btn.Name = "GBtn" & i.ToString() btn.Text = "Push " & i.ToString() btn.Top = btn.Height * i AddHandler btn.Click, AddressOf GBtn_Click Me.Controls.Add(btn) Next End Sub Private Sub GBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) MessageBox.Show(sender.ToString()) Select Case DirectCast(sender, Button).Name Case "GBtn1" Case "GBtn2" End Select End Sub
solidIT.de - under construction Components for Microsoft .Net audittrail, objectcomparer, deepcopy and much more ...