Labels and Mouse
-
I have an array of labels on a form. I want to track which one was clicked on (right click). Anyone know an easy way to do this?
Since you're referencing an array of labels I assume you're not using .NET. In that case, the MouseDown event for the control array would probably be eaisest:
Private Sub Label1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbRightButton Then MsgBox "Label = " & Label1(Index).Caption & " Index = " & Index End If End Sub
-
Since you're referencing an array of labels I assume you're not using .NET. In that case, the MouseDown event for the control array would probably be eaisest:
Private Sub Label1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbRightButton Then MsgBox "Label = " & Label1(Index).Caption & " Index = " & Index End If End Sub
Thanks, but I am using .NET. It appears the compatability wizzard converted these, from the "VisualBasic.Compatability" class, guess I need to look at changing those as well. Any quick suggestions as to how to go about doing this progaramatically? I need to load a bunch of labels on screen (representing slots) and allow the operator to select individual ones. The number and orientation depend on the system configuration at the time and can not be hard coded.
-
Thanks, but I am using .NET. It appears the compatability wizzard converted these, from the "VisualBasic.Compatability" class, guess I need to look at changing those as well. Any quick suggestions as to how to go about doing this progaramatically? I need to load a bunch of labels on screen (representing slots) and allow the operator to select individual ones. The number and orientation depend on the system configuration at the time and can not be hard coded.
First off, the conversion wizard is the devil, just rewrite ;) The concept is very similar:
Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown, Label2.MouseDown If e.Button = Windows.Forms.MouseButtons.Right Then MsgBox("Label = " & sender.Name) End If End Sub
This assumes you have a static "maximum" number of labels that you can keep hidden unless needed and have all of them listed on the event handles. If you need a variable number of labels that you can't predefine then you will need to create a new label control, and assign it to the event handler. I will leave that example to someone a bit more familiar with .NET than myself. -
First off, the conversion wizard is the devil, just rewrite ;) The concept is very similar:
Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown, Label2.MouseDown If e.Button = Windows.Forms.MouseButtons.Right Then MsgBox("Label = " & sender.Name) End If End Sub
This assumes you have a static "maximum" number of labels that you can keep hidden unless needed and have all of them listed on the event handles. If you need a variable number of labels that you can't predefine then you will need to create a new label control, and assign it to the event handler. I will leave that example to someone a bit more familiar with .NET than myself.First off, the conversion wizard is the devil, just rewrite
Agreed, once you have the fundamentals down, I'd suggest a complete rewrite as well.