views:

36

answers:

2

I need a tooltip with custom Font.

I have the following code, and this works... but the tooltip size does not fit the text.

Where is the error?

Public Class KeolisTooltip
  Inherits ToolTip

  Sub New()
    MyBase.New()
    Me.OwnerDraw = True
    AddHandler Me.Draw, AddressOf OnDraw
  End Sub

  Private _Font As Font
  Public Property Font() As Font
    Get
      Return _Font
    End Get
    Set(ByVal value As Font)
      _Font = value
    End Set
  End Property

  Public Sub New(ByVal Cont As System.ComponentModel.IContainer)
    MyBase.New(Cont)
    Me.OwnerDraw = True
    AddHandler Me.Draw, AddressOf OnDraw
  End Sub


  Private Sub OnDraw(ByVal sender As Object, ByVal e As DrawToolTipEventArgs)
    Dim newArgs As DrawToolTipEventArgs

    If _Font Is Nothing Then
      newArgs = e
    Else
      Dim newSize As Size = Size.Round(e.Graphics.MeasureString(e.ToolTipText, Me._Font))
      Dim newBounds As New Rectangle(e.Bounds.Location, newSize)

      newArgs = New DrawToolTipEventArgs( _
         e.Graphics, _
         e.AssociatedWindow, _
         e.AssociatedControl, _
         newBounds, _
         e.ToolTipText, _
         Me.BackColor, _
         Me.ForeColor, _
         Me._Font)
    End If

    newArgs.DrawBackground()
    newArgs.DrawBorder()
    newArgs.DrawText()
  End Sub

End Class
+1  A: 

Size.Round (from the MSDN page)

Converts the specified SizeF structure to a Size structure by rounding the values of the SizeF structure to the nearest integer values.

(my emphasis).

Therefore, if

e.Graphics.MeasureString(e.ToolTipText, Me._Font)

produces values of 23.4 and 42.1 (say) then they will be rounded to 23 and 42 respectively so your tooltip will be slightly too small.

ChrisF
sow, what solution do you propose?
serhio
@serhio - you need to round up to the next highest integer (or even add a bit more padding).
ChrisF
A: 

Could you try to add the resizing logic on the OnResize event in addition to the OnDraw event? I think you will get the correct values on that event. Just try and let know if it works.

Unmesh Kondolikar