Results 1 to 5 of 5

Thread: click on a control

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    439

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: click on a control

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: click on a control

    try this:

    vb Code:
    1. Private Sub Labels_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    2.        Handles Label1.MouseDown, Label2.MouseDown, Label3.MouseDown, Label4.MouseDown, Label5.MouseDown, _
    3.        Label6.MouseDown, Label7.MouseDown, Label8.MouseDown, Label9.MouseDown, Label10.MouseDown
    4.         Label11.Text = CType(sender, Label).Name
    5. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    439

    Re: click on a control

    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?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: click on a control

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width