Link to home
Start Free TrialLog in
Avatar of jagguy
jagguyFlag for Australia

asked on

click on a control

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
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

Mouse events are individual control-oriented. There is a .Click and a .MouseHover event for each control. You can use a single event handler for multiple controls, such as:

    Private ctlCurrent as Control

    Private Sub LabelAll_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseHover, Label2.MouseHover, Label3.MouseHover
        ctlCurrent = sender
    End Sub

You could now reference ctlCurrent in another event handler to find out what label the mouse is over.
Looking again, if you're only concerned about this when you're clicking, then use the .Click event, not the .MouseHover, and the ctlCurrent variable could potentially be inside the Click event handler (if you won't be needing the information in other subs).
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jagguy

ASKER

i have the labels created in a class and each class added to a collection.

it is confusing to set a variable when a mouse is over the label. I drag a button on screen and when i drop this I check to see what label it is over in the button_mouseUp event.

why doesnt this variable set to true when i drag a  button on the label?


Public Class labelArray
    Dim lb As New Label
    Dim mwidth As Integer = 40
    Dim mheight As Integer = 40
    Dim _row, _col As Integer
    Dim mOver As Boolean = False
 
    Public Sub New(ByVal myp As System.Windows.Forms.Panel, ByVal myform As System.Windows.Forms.Form, _
                   ByVal row As Integer, ByVal col As Integer)
        lb.Left = col * mwidth + myp.Width
        lb.Top = row * mheight
        lb.Text = "Empty"
        lb.BorderStyle = BorderStyle.FixedSingle
        lb.Width = mwidth
        lb.Height = mheight
        myform.Controls.Add(lb)
        _row = row
        _col = col
 
        AddHandler lb.MouseHover, AddressOf lb_mousehover
        AddHandler lb.MouseLeave, AddressOf lb_mouseleave
    End Sub
    Private Sub lb_mousehover()
        mOver = True
 
    End Sub
 
    Private Sub lb_mouseleave()
        mOver = False
 
    End Sub
    Property mouseover()
        Get
            Return mOver
        End Get
        Set(ByVal value)
 
        End Set
    End Property

Open in new window

Avatar of jagguy

ASKER

what happens is that there is a contro over the label stopping the event from firing.

is there a way around this?
Avatar of jagguy

ASKER

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?