assigning object to button.tag - vb.net

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)

Related

A problem on right-click menu using Vb.Net

I want to add a right click menu to a dynamic added button by using ContextMenuStr. My code are as follows:
Public Class Form1
Private mybutton As New Button
Private i As Integer
Private mybutton_cms As ContextMenuStrip = New ContextMenuStrip()
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If e.Node.Text = "addbutton" Then
***Dim mybutton As New Button***
Me.Controls.Add(mybutton)
i = i + 1 'give a ID to the dynamic added button
mybutton.Left = 200
mybutton.Top = 200
mybutton.Name = "mybutton"
AddHandler mybutton.MouseMove, AddressOf mybutton_MouseMove
ElseIf e.Node.Text = "deletebutton" Then
Me.Controls.RemoveByKey("mybutton")
i = i - 1 'delete the the ID of button
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tsm As ToolStripMenuItem = New ToolStripMenuItem("change icon")
mybutton_cms.Items.Add(tsm)
mybutton.ContextMenuStrip = mybutton_cms
End Sub
Private Sub mybutton_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
OBJ_Resize.ResizeOBJ(sender, e, Me)
Dim file As New IO.StreamWriter("data2.xml", OpenMode.Append)
file.WriteLine("width=" & sender.width)
file.WriteLine("height=" & sender.height)
file.WriteLine("x=" & sender.left)
file.WriteLine("y=" & sender.top)
file.Close()
End Sub
Private Sub mybutton_changeicon(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
changeIcon.changeicon(sender, e)
End Sub
End Classa
there is a bug that if I delete the Code referenced with *,namely :
Dim mybutton As New Button
when I run the code, there is no right click menu on the button dynamiclly added.
And if I add this code,there is only one dynamical added button on the form1. But the rigtht click menu will show.
What I want to do is click “addbutton” once to get a button, and each button can have a right-click menu
What's wrong with my code?
Beside,could you plese tell me How to add left-click events to the right menu item?
Thanks!

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 name of the button that triggered the event

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);

Trigger an event with one sub for multiple controls

I have 10 panels on my form, and when you hover them, their color changes. I have 10 private subs like so...
Private Sub pnl2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnl2.MouseHover
pnl2.BackColor = Color.WhiteSmoke
End Sub
This code is repeated for each panel with the only difference being it's name, how can I do this more efficiently? as it is very repetitive.
Add them at the handler statement appending each by a comma. The sender object is the panel in question so cast it to change it's properties.
Private Sub pnl2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnl2.MouseHover, pnl3.MouseHover 'etc
Dim pnl As Panel = CType(sender, Panel)
pnl.BackColor = Color.WhiteSmoke
End Sub

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