Results 1 to 12 of 12

Thread: Which control is under the mouse?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    Which control is under the mouse?

    I was thinking of the way to detect which control is under the mouse. I couldnt find an easy way.
    I think it can be done by:
    1-Detecting the mouse position.
    2-Looping through the controls of the form and if the mouse position falls in the bounds of the control then the mouse is on that control.
    But this is a little time consuming and not desireable esp in a very populated form. Any better idea?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    write a mousehover event and wire up the mousehover event of all controls to it. sender parameter will be the control that the mouse is over.

    Code:
     
    
        Private Sub MouseHover1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseHover, Label1.MouseHover
            MessageBox.Show(sender.Name)
        End Sub
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Excuse me. MouseEnter might be better than MouseHover
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Thanks Cander
    I was thinking to make it more dynamic, and that was looping through all controls at form_load event and addhandler for their Mouse_Enter event. What you think about that?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    i was actually looking into that. I m tryin to create a loop that goes through each control on the form then adds the handler to it. Sorta like this

    VB Code:
    1. Dim a As Object
    2.  
    3.         For Each a In Me.Controls
    4.             If a.GetType.ToString() <> "Form" Then
    5.                    AddHandler CType(a, a.GetType).MouseEnter, AddressOf MouseHover1
    6.             End If
    7.         Next

    Problem is, it wont accept the a.GeType from within the CType function
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    if nothing else, you could Select Case the a.GetType.ToString for things like Button, Label, etc. and just do a CType(a, Button).MouseEnter for Button, same for label, etc.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    and why excluded 'Form'?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    I didnt want it to Messagebox everytime i moved the mouse. No other reason
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    oh boy..I figured it out

    VB Code:
    1. For Each a In Me.Controls
    2.                 AddHandler CType(a, Control).MouseEnter, AddressOf MouseHover1
    3.         Next
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I didnt want it to Messagebox everytime i moved the mouse. No other reason
    I need to send the control which is under the mouse to another event and that inclueds the 'Form' itslef. By the way the form itself is not included in its control collections. So I should write the event for the form that hanles MyBase.MouseEnter and address all of other controls to that event.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  11. #11
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Well since form is just 1 object, I see no reason why shouldnt just straight code in the Forms MouseWhatever event. And use the wired up event for all other controls.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Thats just what i meant, write the event for form and
    AddHandler CType(a, Control).MouseEnter, AddressOf Form1_MouseEnter
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

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