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
Related
Im making a class that will show a form with some text and a OK button. But I can't seem to find how to handle clicking the OK button. Here is the code that im having problems with:
Private Sub ok_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myForm.okButton.Click
'code
End Sub
Runtime form is called myForm
Runtime button is called okButton
How I can fix this?
You'd use AddHandler:
' ... run-time control is created ...
Dim btn As New Button
btn.Text = "Hello World!"
AddHandler btn.Click, AddressOf ok_click
In your existing handler, you do NOT use the "Handles" keyword on the end. If you need a reference to the button, use the "sender" parameter:
Private Sub ok_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
' ... do something with "btn" ...
btn.Enabled = False
End Sub
I have this six button
After i press one button, for example i press settings it will open an new windows after i close that windows the button remain pressed.
Here are the settings button code:
Private Sub btn_SETTINGS_MouseEnter(sender As System.Object, e As System.EventArgs) Handles btn_SETTINGS.MouseEnter
btn_SETTINGS.ForeColor = Color.White
End Sub
Private Sub btn_SETTINGS_MouseLeave(sender As System.Object, e As System.EventArgs) Handles btn_SETTINGS.MouseLeave
btn_SETTINGS.ForeColor = SystemColors.HotTrack
End Sub
Private Sub btn_SETTINGS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SETTINGS.Click
OpenSettings()
End Sub
Any suggestion what can i do to fix this problem.
Okay, this problem seems related to Focus that got set to the button when you pressed it. Very easy and quick fix of this problem is, change the focus of control to some other control. On the same side, add one label control which is of Hidden type say lblHidden. So when you're doing following code then change focus.
Private Sub btn_SETTINGS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SETTINGS.Click
OpenSettings()
Me.ActiveControl = lblHidden
End Sub
This will change the focus to hidden control. However if you are doing any formatting change of button on click event then revert it back to original in above even.t
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);
Let me explain further. Let's say I have 20 buttons on a form, and in the all of the button's click event I want to call a specific function, instead of placing the calling to that function in each click event, is there a way to call it from any of the click events without having to place the code in each click event?
Hope that makes sense.
Yes, just add the click event for each button to the end of the Handles clause.
Private Sub My_Sub(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
' do stuff
End Sub
If you have a lot of buttons or a dynamic number of buttons, you could use a recursive method to add the same event handler for each. This will take care of all buttons, even those inside group boxes or other containers.
First, create the method you want each button click to call.
Private Sub bt_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
'
' Your code goes here
'
End Sub
Second, create a method to recursively find all the button controls and add the event handler.
Private Sub AddEventHandler(RootControl As Control)
For Each c As Control In RootControl
If c.HasChildren Then
AddEventHandler(c)
End If
If TypeOf c Is Button Then
AddHandler c.Click, AddressOf bt_ButtonClick
End If
Next
End Sub
Lastly, in your form load event, add this line:
AddEventHandler(Me)
Wire the one sub to the click event for every button.
Private Sub AnyButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
End Sub
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)