I have been working on my first custom user control. The custom control inherits UserControl. The user control contains a combination of drawn rectangles, circles and a icon image. I would like to display a tool tip when the user hovers the mouse over the image portion of the control. The image was created by using drawicon. I then have a mouse move event in my control that detects when the mouse is over the icon. That logic all works properly. What I am having a problem with is that after the tool tip disappears, it erases the part of the control is was hovering over. I am trying to figure out the proper way to repaint the control but I am not having any luck.
What I have tried:
Here is how I am painting the control:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim rect As System.Drawing.Rectangle = e.ClipRectangle
Dim g As Graphics = e.Graphics
g.SmoothingMode = SmoothingMode.HighQuality
'Draw the Machine Bed
DrawBed(g, rect)
'Draw and position the Machine Head
DrawHead(g, rect, _HeadPosInInches)
End Sub
Private Sub DrawBed(ByVal g As Graphics, ByVal CtlRect As System.Drawing.Rectangle)
'Draws the bed
'Size the bed based on the control size
_BedWidth = CInt(CtlRect.Width * _BedWidthFactor)
_BedHeight = CInt(CtlRect.Height * _BedHeightFactor)
'Center the bed within the control
_BedYBorder = CInt((CtlRect.Height - _BedHeight) / 2)
_BedXBorder = CInt(CtlRect.Width - _BedWidth)
'Create a new rect for the bed
Dim BedXLoc As Integer = 0
If HeadTravelDir = 1 Then
BedXLoc = _BedXBorder
Else
BedXLoc = 0
End If
Dim BedRect As New Rectangle(BedXLoc, _BedYBorder, _BedWidth, _BedHeight)
'Fill the Bed
Using Brush1 As New SolidBrush(_BedColor)
g.FillRectangle(Brush1, BedRect)
End Using
'Create the Bed border
Using Pen1 As New Pen(Color.Black, 1)
g.DrawRectangle(Pen1, BedRect)
End Using
End Sub
Here is how I am drawing the icon image on the screen:
Dim TargetX As Integer = 0
Dim TargetY As Integer = 0
Dim TargetWidth As Integer = _BedYBorder
Dim TargetHeight As Integer = _BedYBorder
If _HeadTravelDir = 1 Then
TargetX = HeadRect.X + HeadRect.Width + 5
Else
TargetX = HeadRect.X - 5 - _BedYBorder
End If
Dim DashboardIcon As New Icon("U:\photos\DashboardIcons\Question.ico")
Dim TargetRect As New Rectangle(TargetX, TargetY, TargetWidth, TargetHeight)
IconRect = TargetRect
g.DrawIcon(DashboardIcon, TargetRect)
Here is how I am detecting when the mouse if over the icon image:
Private Sub ctlGantry_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
'Checks to see if the mouse if over the icon
If IconRect.Contains(e.Location) Then
Debug.WriteLine(e.Location.ToString & " is Over the Icon for " & Me.Name)
_IconMouseOver = True
'Me.ToolTip1.Show("test", Me)
Else
_IconMouseOver = False
'Me.ToolTip1.Hide(Me)
'Me.Invalidate()
End If
End Sub
I have tried displaying the tooltip in both the mousemove and the mousehover events but I get similar results. If I try to invalidate when the mouse is not over the icon, I get a lot of flickering. The background control also doesn't repaint when I move the mouse in a different position over the icon. I am not sure if this is related to the same issue but I also have a problem where the icon will no longer reappear after I hover on the control for a few seconds and then comeback and rehover. If I repaint the control and try it again, then the tooltip will reappear. I am very close to being able to wrap this control up but I need to figure this out first. Any ideas?