Get the name of the button that triggered the event - vb.net

I have an event handler that handles the click event of multiple buttons:
Private Sub primeHandler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _2s.Click, _3s.Click, _4s.Click, _5s.Click, _6s.Click
End Sub
_2s, _3s, etc are all buttons.
Now I need a way to determine which button triggered the event and also get the button's name as string. Any way to do that? Thanks

You can cast sender to type Button and access the Name property.
Private Sub primeHandler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _2s.Click, _3s.Click, _4s.Click, _5s.Click, _6s.Click
Dim myButton As Button = CType(sender, Button)
Dim myName As String = myButton.Name
End Sub

Use sender - it's what it's designed to do.
MessageBox.Show((sender as Button).Name);
If you're going to use it more than once, assign it to a variable to make it easier.
var button = (sender as Button);
MessageBox.Show(button.Name);

Related

Get text of self object in vb.net

Let's say I have a button with the text 'A'.
When I click this button, I want a messagebox to appear with the text of what name of the button that was clicked.
I tried setting it to MessageBox.Show(Me.Text) but that just gives me the form name.
How can I refer to the text of button I just clicked?
If you have to handle multiple button click try following method
Private Sub btn_a_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn_a.Click _
,btn_b.Click 'you can add other buttons click event here(ex. btn_c,btn_d etc)
Dim objButton As Button = DirectCast(sender, Button)
MessageBox.Show(objButton.Text)
End Sub
DirectCast() vs. CType()
The reason why MessageBox.Show(Me.Text) didn't work is that Me refers the class containing the code, in this case, the Form).
If your button's event handler only handles one button, you can just hard code the buttons name like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(Button1.Text)
End Sub
If the event handler can handle more than one button, you can use the sender argument to reference the button being clicked:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim but as Button = CType(sender, Button)
MessageBox.Show(but.Text)
End Sub

Get the ID/Name of a button in a click event. VB.NET

I have an event in VB.NET to handle several button clicks at once. I need to know which button from the selection kicked off the event. Any ideas how to do this? My code is below:
Private Sub Answer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnswer1.Click, btnAnswer2.Click, btnAnswer3.Click, btnAnswer4.Click
'output button ID that caused event
End Sub
I've tried sender.Id, e.Id, sender.name, e.name. None of them work
You have to cast the sender to the object type expected.
Dim btn As Button = CType(sender, Button)
Then you can access what you need.
Try CType(Sender, Button).Name. Sender is an Object you need to cast to the calling Type in this case Button. If you need more properties from the Sender then use U1199880 's answer. But usually when I am trying to handle multiple clicks I will use the Tag property, assign an index to it. Something like this.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Dim index As Integer
If Not Integer.TryParse(CType(sender, Button).Tag.ToString, index) Then Exit Sub
Select Case index
Case 0
Case 1
Case 2
....
End Select
End Sub
Even simpler:
If sender is btnAnswer1 then ...

assigning object to button.tag

I created a Sub I want to run everytime one of two buttons is clicked. I added the handles of both buttons to the sub so that clicking either one will fire the subroutine.
I placed listview object A in buttonA.tag, and listview object B in buttonB.
When the button is clicked I do my best to extract the listview instance tucked into the button's tag. The problem is there is no instance in the tag. It is simply "nothing."
Private Sub Execute(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click, btnB.Click
Dim buttonSender As Button = Nothing
buttonSender = CType(sender, Button)
Dim btnListView As ListView = buttonSender.Tag
End Sub
-------------------Edit-1
Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.btnA.Tag = Me.lvA
Me.btnB.Tag = Me.lvB
End Sub
Your time is appreciated.
Try this,
Dim buttonSender As Button = CType(sender, Button)

How to check focused TextBox in vb.net winforms?

I have multiple textbox in a form. How do I know what textbox the cursor currently is?
Trying to do something like this:
If TextBox2.Focus() = True Then
MessageBox.Show("its in two")
ElseIf TextBox3.Focus = True Then
MessageBox.Show("its in three")
End If
But I think its not working.
TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)
In fact, all form controls have the Focused property.
I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.
Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() focussedTextBox = textBox
End If
Next
End Sub
This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:
If Not focussedTextBox Is Nothing Then
...
End If
Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.
Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.
You can do two things:
Make a combined Focus event for all TextBoxes and check its Sender object.
Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
Dim currTextBox As TextBox = sender
If currTextBox.Equals(TextBox2) Then
MessageBox.Show("it's in two")
ElseIf currTextBox.Equals(TextBox3) Then
MessageBox.Show("it's in three")
End If
End Sub
OR
Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.
Dim str As String
Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
str = "two"
End Sub
Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter
str = "three"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("it's in " & str)
End Sub

A better way to group actions of similar buttons?

say I have a Button1 subroutine
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim buttonText As String = Button1.Text
someOtherRoutine(buttonText)
End Sub
I have a lot of such buttons in my main form. They all do the same thing like this. get the text and pass to some other routine. If i have 20 buttons, then i will have 20 such subroutines. Is there a better (or standard way) to do this without creating that many subroutines?
thanks
You can use this one subroutine to handle all of the buttons:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click, _
Handles Button2.Click, _
...
Handles Button20.Click
Dim myButton As Button = sender
Dim buttonText As String = myButton.Text
...
End Sub
You can use AddHandler to add the same event handler for every button or you can use a comma separated list in the Handles clause.
Sub EventHandler() Handles Obj.Ev_Event, Obj2.Ev_Event
' Handle the event.
MsgBox("EventHandler caught event.")
EndSub