Resolved VS2008 Show buttons in a region

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I have a Rectangle (the class) that I'm using to define a region on a form and when the mouse enters that region it shows 2 buttons (which are on the form and within the bounds of the rectangle) and I would like the buttons to remain visible when the mouse is over either of the 2 buttons then when the mouse leaves the region or leaves the form they are set to invisible again. Here's the code I have so far:
VB.NET:
    Private m_TitleColor As Color
    Private Const m_TitleHeight As Integer = 30I

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        Using br As New SolidBrush(m_TitleColor)
            e.Graphics.FillRectangle(br, Me.TitleRect())
        End Using
    End Sub

    Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
        MyBase.OnMouseLeave(e)
        Call TitleToggle(False) 'Left the form, hide the buttons
    End Sub

    Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseMove(e)
        Call TitleToggle(Me.TitleRect().Contains(e.Location))
    End Sub

    Private ReadOnly Property TitleRect() As Rectangle
        Get
            Return New Rectangle(0I, 0I, Me.ClientSize.Width, m_TitleHeight)
        End Get
    End Property

    Private Sub TitleToggle(ByVal isVisible As Boolean)
        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is Button Then ctrl.Visible = isVisible
        Next ctrl
    End Sub
The problem is when the mouse enters the bounds of the button, the form's MouseLeave event fires and thus my buttons are hidden which means the user can't click either of them either. Any ideas?
 
Never mind, I realized what I needed to do:
VB.NET:
    Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
        MyBase.OnMouseLeave(e)
        If Not Me.TitleRect.Contains(Cursor.Position.X - Me.Left, Cursor.Position.Y - Me.Top) Then Call TitleToggle(False)
    End Sub
 
Back
Top