0

i got 59 labels (they describe places in a storage system). With a context menu i want to lock or unlock each of those places for the user. So in my contextmenu i got two items: Lock and Unlock. If one label (place) is locked by the user it should send an bit to a plc (but that is not my main problem), and the same for unlock.

Is there any opportunity to do a loop, to find out on which label the contextmenu has been opened? (MouseDown Event)

Here my code so far:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
       Handles MyBase.Load
    Me.ContextMenuStrip = ContextMenuStrip1
End Sub

Private Sub Label1_MouseDown(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown, Label2.MouseDown, Label3.MouseDown, Label4.MouseDown, .............. , Label59.MouseDown
    ContextMenuStrip1.Tag = sender
End Sub

Private Sub LockToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LockToolStripMenuItem.Click
    'send bit to PLC (sender)
End Sub

Private Sub UnlockToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UnlockToolStripMenuItem.Click
    'send bit to PLC (sender)
End Sub
End Class

Sorry for the bad english.

  • 2
    [Get the SourceControl of a DropDownMenu](https://stackoverflow.com/a/53263702/7444103) -- If the ToolStripItem is direct child of the ContextMenuStrip, just get the SourceControl of the owner. – Jimi Jul 09 '21 at 10:56
  • it says Owner= System.Windows.Forms.Label , but without the number (e.g. label1, label2, etc) – HappyHippo73 Jul 09 '21 at 12:07
  • The *number*? That's the actual reference of the Control that caused the ContextMenuStrip to open. You don't need any *number*. Just as in the Q&A code linked, e.g., `CType(LockToolStripMenuItem.Owner, ContextMenuStrip).SourceControl.BackColor = Color.Red` or, in the `LockToolStripMenuItem` Click handler: `dim theLabelClicked = CType(LockToolStripMenuItem.Owner, ContextMenuStrip).SourceControl` etc. – Jimi Jul 09 '21 at 12:29
  • i looked the SourceControl command up in microsoft docs, there was a line with fruitContextMenuStrip.Items.Add("Source: " + c.GetType().ToString()); and as i tried it, it said "Source: System.Windows.Forms.Label". So the SourceControl command can#t be transferred to string with the number of the label? – HappyHippo73 Jul 09 '21 at 12:46
  • 1
    For goodness' sake! Put some thought into it. Where is this number? Where did you put it in the first place? Was it the `Name` property of the `Label`? If so, where do you think you should get it back from? Could it be the `Name` property? What's the point of calling `ToString` if what you actually want is the `Name`? – jmcilhinney Jul 09 '21 at 12:55
  • That said, what do you want the number for anyway? If you already have the `Label` itself from which to get this number, what can you do with the number that you couldn't do with the `Label` itself? Apart from anything else, you specifically asked in the question how to tell which `Label` the menu was opened on and that has been explained to you. There's nothing about a number in the question so you are now asking a new question in a comment. DO NOT do that. Edit your question and ask the actual question you want answered. Provide ALL the information relevant to that question. – jmcilhinney Jul 09 '21 at 12:59
  • I'm sorry. Maybe my bad English is the reason i didn't understood it the first time. The ToString Was just a little test for me to see what it would give back. With the numbers i meant Label1, Label2, etc. On which label the contextmenu was opened. But i think i can work it out now. Thanks – HappyHippo73 Jul 09 '21 at 20:36
  • Is your problem solved? If so, please consider sharing your solution here and [accepting it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Xingyu Zhao Jul 12 '21 at 06:34

0 Answers0