is there a way to click anywhere on a form and detect what control you are clicking or what control you are over?
I have a screen full of labels and i need to know what label my cursor is over
Printable View
is there a way to click anywhere on a form and detect what control you are clicking or what control you are over?
I have a screen full of labels and i need to know what label my cursor is over
Just use a single event handler to handle the appropriate event for all of the Labels. If you only want to know which Label you're over when the mouse stops then handle the MouseHover event. If you always want to know what Label you're over then handle the MouseEnter and MouseLeave events. As always, the sender parameter is a reference to the object that raised the event, i.e. the Label you're interested in.
try this:
vb Code:
Private Sub Labels_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles Label1.MouseDown, Label2.MouseDown, Label3.MouseDown, Label4.MouseDown, Label5.MouseDown, _ Label6.MouseDown, Label7.MouseDown, Label8.MouseDown, Label9.MouseDown, Label10.MouseDown Label11.Text = CType(sender, Label).Name End Sub
what happens is that there is a control over the label stopping the label mouse_hover event from firing.
how can i get the label events to fire if another control is placed over it?
is there a way around this?
Why would there be another control over a Label? Not impossible I suppose, but odd. You won't be able to get an event from a Label if the mouse doesn't actually hover over the Label. In that case you'd have to perform some calculations of your own to determine whether the mouse pointer was within the bounds of each Label. Methods like Control.PointToScreen and .PointToClient or Rectangle.Contains will be useful.