Is there a way to limit the width of a tooltip?

bdinnocenzo

Active member
Joined
Oct 22, 2009
Messages
38
Location
Massachusetts
Programming Experience
5-10
I am using tooltips to show information that overflows a label. The problem is that when the information is really long it spans the entire width of the screen and is difficult to read, Is there any way to limit the size of the tooltip?

I was thinking one way may be to insert linefeeds at points beyond a certain width -- messy, but it would work. Any suggestions?

-Bill

P.S. Happy New Year!
 
Here is a function that will do that for you. Place this in your form's code.

VB.NET:
    Public Function SetWidth(ByVal str As String, ByVal fnt As Font, ByVal width As Integer) As String
        Dim rstr As String = Environment.NewLine & str
        Dim pos As Integer = 1
        Dim pos2 As Integer
        While pos < str.Length
            pos = InStr(pos, rstr, " ") + 1
            If pos = 1 Then
                Exit While
            End If
            pos2 = InStrRev(rstr, Environment.NewLine, pos)
            If Me.CreateGraphics.MeasureString(Mid(rstr, pos2, pos - pos2 + 1), fnt).Width > width Then
                rstr = rstr.Insert(InStrRev(rstr, " ", pos - 1), Environment.NewLine)
            End If
        End While
        Return Mid(rstr, 2, rstr.Length - 1)
    End Function
 
Thanks! I was just about to code that...you saved me some time...I appreciate that!

-Bill
 
It would be convenient if e.ToolTipSize in Popup event worked properly, currently the size can be set, but the placement can't so the tooltip appears off location after it is resized.

Pirahnaplant, when you CreateGraphics you must also Dispose it.
 
Back
Top