contextmenustrip opening event determining sender - vb.net

I have 30 labels. They can have any value I want.
I need to be able to assign one context menu to them all then determine which label was clicked in order to use my x variable.
Private Sub Label_Click(sender As Object, e As MouseEventArgs) Handles Label1.MouseClick, Label2.MouseClick, Label3.MouseClick, Label4.MouseClick, _
Label5.MouseClick, Label6.MouseClick, Label7.MouseClick, Label8.MouseClick, Label9.MouseClick, Label10.MouseClick, Label11.MouseClick, _
Label12.MouseClick, Label13.MouseClick, Label14.MouseClick, Label15.MouseClick, Label15.MouseClick, Label16.MouseClick, Label17.MouseClick, _
Label18.MouseClick, Label19.MouseClick, Label20.MouseClick, Label21.MouseClick, Label22.MouseClick, Label23.MouseClick, Label24.MouseClick, _
Label25.MouseClick, Label26.MouseClick, Label27.MouseClick, Label28.MouseClick, Label29.MouseClick, Label30.MouseClick
Dim x As String = sender.Text
xmlinteraction.appCall(x)
End Sub
I received awesome help the other day passing variable into contextmenustrip
But I am too new to put it all together and make it work. I understand what we are trying to do, but not all the syntax. Please help.
Jay,
Here is what I put together from the code you gave me. Is this what you were thinking? I feel like I missing something still and further clean the code. Possibly removing the case statements.
Private Sub rcmenuOption(x, y)
' x is equal to what the menu item was clicked
' Create case stament for that to call the correct xmlinteraction passing in y
Select Case x
Case "Copy Link"
copyClipboard(y)
End Select
End Sub
Private Sub rcmenuClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles rcmenu.ItemClicked
' Get the Label clicked from the SourceControl property of the clicked ContextMenuStrip.
Dim contextMenu = DirectCast(sender, ContextMenuStrip)
Dim label = DirectCast(contextMenu.SourceControl, Label)
Dim var2 As String = label.Text
' Get the clicked menu strip and update its Text to the Label's Text.
Dim toolStripItem = e.ClickedItem
Dim var As String = toolStripItem.Text
rcmenuOption(var, var2)
End Sub
contextmenustrip opening event determining sender

OK, you have a number of Labels on a form and all of them use the same ContextMenuStrip (all the Labels have their ContextMenuStrip property set to the same ContextMenuStrip control).
When the user right clicks a Label and selects a menu item, you want that menu item's Text to change to the clicked Label's Text.
You can do this using your ContextMenuStrip ItemClicked event handler. Use the handler's sender and ToolStripItemClickedEventArgs parameters to get the Label's Text and a reference to the ToolStripItem clicked.
Private Sub ContextMenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStrip1.ItemClicked
' Get the Label clicked from the SourceControl property of the clicked ContextMenuStrip.
Dim contextMenu = DirectCast(sender, ContextMenuStrip)
Dim label = DirectCast(contextMenu.SourceControl, Label)
' Get the clicked menu strip and update its Text to the Label's Text.
Dim toolStripItem = e.ClickedItem
toolStripItem.Text = label.Text
End Sub

Related

Button Array - how to pass a parameter to shared handler

I have a bit of code where i have a dynamically created array or buttons with staff pictures on them, as well as the staff's name. I've added one handler to handle any button click from any of the buttons. where i am stuck is, if you look at the code below, it all works fine, and if you click any of the buttons you get the "aha" test message. but i want the name of the staff clicked on (so btnArray(i).Text) to be passed to the handler for further processing. I tried adding a ByVal parameter to the handler but that caused an error. what's the correct way to do this? As i said, the code below works for me, i just am at a loss as to how to add the extra functionality.
Dim btnArray(staffcount) As System.Windows.Forms.Button
For i As Integer = 1 To staffcount - 1
btnArray(i) = New System.Windows.Forms.Button
btnArray(i).Visible = True
btnArray(i).Width = 80
btnArray(i).Height = 101
btnArray(i).BackgroundImage = Image.FromFile(picloc(i))
btnArray(i).BackgroundImageLayout = ImageLayout.Stretch
btnArray(i).Text = staffname(i)
Dim who As String
who = btnArray(i).Text
AddHandler btnArray(i).Click, AddressOf Me.theButton_Click
btnArray(i).ForeColor = Color.White
btnArray(i).TextAlign = ContentAlignment.BottomCenter
Dim fnt As Font
fnt = btnArray(i).Font
btnArray(i).Font = New Font(fnt.Name, 10, FontStyle.Bold)
FlowLayoutPanel1.Controls.Add(btnArray(i))
Next i
End Sub
Private Sub theButton_Click()
MsgBox("aha")
End Sub
First, correct the signature of your shared handler.
Private Sub theButton_Click(sender As Object, e As EventArgs)
End Sub
Once that is done getting the text of the button clicked is a simple matter.
Private Sub theButton_Click(sender As Object, e As EventArgs)
Dim textOfButtonClicked As String = DirectCast(sender, Button).Text
MessageBox.Show(textOfButtonClicked)
End Sub
The sender is the button that was clicked. Since signatures use objects for the sender the DirectCast 'changes' it to button and you then can access the .Text property of the button.
If there are more manipulations you want to perform on the clicked button you could do it this way
Private Sub theButton_Click(sender As Object, e As EventArgs)
Dim whBtn As Button = DirectCast(sender, Button) ' get reference to button clicked
Dim textOfButtonClicked As String = whBtn.Text
MessageBox.Show(textOfButtonClicked)
'e.g. change the color
whBtn.BackColor = Color.LightYellow
End Sub

Visual Basic - Grouped radio buttons

Basically what i'm wanting to work is for the text of the selected radio button to be inserted into a ListView that i already have.
Here is my code (Listview):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles AddButton.Click
Dim Col1 As String = ComboBox1.Text
Dim Col2 As String =
Dim Col3 As String = ComboBox3.Text
Dim order As New ListViewItem
order.Text = Col1 'Adds to First column
order.SubItems.Add(Col2) 'Adds to second column
order.SubItems.Add(Col3) ' Adds to third column
ListView1.Items.Add(order)
End Sub
If i put RadioButton1.Text In DIM Col2 and select it when it runs then it displays it but i would like it so that it finds out which radio button is selected and displays the correct text. I Have four radio buttons all in a group called GroupBox1 and each radio button is RadioButton1, 2, 3 etc
The Combo boxes are getting used as the same but i need some sort of radio button in my program. Any help is appreciated.
With two RadioButtons inside one GroupBox, and one TextBox:
Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) _
Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
If CType(sender, RadioButton).Checked Then
Dim rButton As RadioButton =
GroupBox1.Controls.
OfType(Of RadioButton)().
Where(Function(r) r.Checked = True).
FirstOrDefault()
Me.TextBox1.Text = CType(sender, RadioButton).Text
End If
End Sub
When a RadioButton is clicked, the TextBox text gets updated. I don't understand how you want to insert into the ListView, so just take the text and put it where you need it.

Update text in dynamically created label

I'm working on a proof of concept type situation that will eventually be tied to a scheduling database. As a Test I created this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'AddButton("test")
addLots()
End Sub
Private Sub AddLots()
Dim x As Integer
For x = 0 To 10
Dim b As New Button
Dim newLabel As New Label
newLabel.Location = New Point(100, x * 20)
newLabel.Name = x
newLabel.BorderStyle = BorderStyle.Fixed3D
newLabel.Text = newLabel.Name
Me.Controls.Add(newLabel)
Me.Controls.Add(b)
b.Location = New Point(20, x * 20)
b.Text = x
b.Tag = x
b.Name = x
AddHandler b.Click, AddressOf Button_Click
Next
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim B As Button = sender
MsgBox(B.Name)
End Sub
For this proof of concept, I simply want label 1 text to be updated when I press button 1 seems like a simple process but it's kicking my butt.
As for any object, to affect a Label you will need a reference to it. As it stands, the only reference you have is via the Controls collection of the parent control you added the Label to, i.e. the form itself. You could loop through the Controls of the form and as soon as you find a Label then you know you have the first one, or you could call OfType and First or FirstOrDefault. That assumes that there are no other Label controls on the form.
You might also consider using a dedicated parent control so that you know it will only contain the Label controls you created at run time. The obvious choice would be a TableLayoutPanel because it will handle the layout for you too.
If accessing the dynamic controls via a Controls collection is an issue then keep your own collection. Declare a member variable of type List(Of Label) and add each Label you create to it. You can then access your control from that collection and know that there are no other controls in there to get mixed up with.
By the way, if you're creating those controls at run time then they won't be automatically disposed when the form is. Make sure that you dispose them yourself and also use RemoveHandler for each AddHandler you used.
Actually, looking closer at your code, I just realised that there's a 1:1 correspondence between the Button and Label controls. It would make sense to use that. Two options are to assign the corresponding Label to the Tag of each Button or else use a Dictionary(Of Button, Label) assigned to a member variable to store the relationships. That way, you can then use the sender in the event handler, which will be the Button that was clicked, to get the corresponding Label.
Option 1.
Creating the Label:
Dim btn As New Button
Dim lbl As New Label
btn.Tag = lbl
In the event handler:
Dim btn = DirectCast(sender, Button)
Dim lbl = DirectCast(btn.Tag, Label)
Option 2.
At class level:
Private labelsByButton As New Dictionary(Of Button, Label)
Creating the Label:
Dim btn As New Button
Dim lbl As New Label
Me.labelsByButton.Add(btn, lbl)
In the event handler:
Dim btn = DirectCast(sender, Button)
Dim lbl = Me.labelsByButton(btn)
I added this to the button click event. Doesn't seem very efficient as I will eventually have 30-40 buttons and controls on the form but it works.
Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim B As Button = sender
Dim lblToChange As Integer = B.Name
For Each objCtrl As Control In Me.Controls
If TypeOf objCtrl Is Label Then
Dim Lbl As Label = DirectCast(objCtrl, Label)
If Lbl.Name = lblToChange Then
Lbl.Text = "This ONe"
End If
End If
Next
End Sub

Unselect listview item when hovering in listview background

I am needing some help with a listview control that uses large icon view. I have icons in my listview control that when I hover over them, it displays a custom tooltip (not the in-built listview tooltip).
My problem is that when I hover into the background of the listview, it doesn't de-select the selected item. I would also like to cut the tooltip short (if possible) so if the tooltip is meant to be visible for 5 seconds and someone hovers into the background of the listview before this time, then the item deselects and the tooltip disappears.
My code currently looks like this:
Private Sub lsvStores_ItemMouseHover(sender As Object, e As ListViewItemMouseHoverEventArgs) Handles lsvStores.ItemMouseHover
Dim storeID As String = e.Item.Name
ShowStoreDetailsTooltip(storeID, sender, e.Item.Position.X, e.Item.Position.Y - 80)
End Sub
Private Sub lsvStores_MouseHover(sender As Object, e As EventArgs) Handles lsvStores.MouseHover
lsvStores.SelectedItems.Clear()
End Sub
Private Sub ShowStoreDetailsTooltip(ByVal Code As String, ByVal Obj As Control, ByVal XPos As
ttpStoreDetails.ToolTipTitle = StoreName
ttpStoreDetails.IsBalloon = True
ttpStoreDetails.Show(String.Empty, Obj, 0)
ttpStoreDetails.Show(tmpString, Obj, XPos, YPos, 5000)
End Sub
Any help appreciated thanks.
Using some code from this answer, we can get the item currently under the mouse cursor using ListView.GetItemAt(), then see if it's Nothing. If it is, then you'll do whatever you need to to make your Tooltip disappear.
Private _item As ListViewItem = Nothing
Private Sub lsvStores_MouseMove(sender As Object, e As MouseEventArgs) Handles lsvStores.MouseMove
Dim item As ListViewItem = lsvStores.GetItemAt(e.X, e.Y)
If item Is Nothing Then
If _item IsNot Nothing Then
_item.Selected = False
End If
_item = Nothing
ttpStoreDetails.Hide(sender)
End If
End Sub

VB.NET ComboBox results selected item

Hi I have a vb windows form application that has a ComboBox from the form1 I have some code that reads some registry and adds item results to combobox. I would like to select one of the results and run a start process. My problem is where do I put the code when item is selected then do something and how to I determine what has been selected?
My Code to query registry keys
Dim Key, Reader As RegistryKey, Y As String
Key = Registry.LocalMachine.OpenSubKey("SOFTWARE\AppStream\AppMgr\Shortcuts", False)
For Each X In Key.GetSubKeyNames
Reader = Registry.LocalMachine.OpenSubKey("SOFTWARE\AppStream\AppMgr\Shortcuts\" & X, False)
If Reader.GetValueNames().Contains("AppTitle") Then
Y = Reader.GetValue("AppTitle")
If Not ComboBox1.Items.Contains(Y) Then ComboBox1.Items.Add(Y)
End If
If i do somehting like this, it just shows a blank messagebox and I have not selected that text from combobox yet.
If ComboBox1.SelectedText Then
MessageBox.Show(ComboBox1.SelectedText())
End If
You subscribe to the SelectedIndexChanged event writing a method like this
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim comboBox As comboBox = CType(sender, comboBox)
' Caution, the event could be called also when there is nothing selected
if combBox.SelectedItem IsNot Nothing Then
Dim curValue = CType(combBox.SelectedItem, String)
'do your stuff with the selected key'
End If
End Sub
if combBox.SelectedItem IsNot Nothing Then
Dim cmbselected As String = DirectCast(DirectCast(DirectCast(DirectCast(combBox, System.Windows.Controls.ComboBox).SelectedValue, System.Object), System.Data.DataRowView).Row, System.Data.DataRow).ItemArray(0)
End If