Click here to Skip to main content
13,198,068 members (66,663 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 4 May 2005

World's Easiest Way to Create Multi-Line Tooltips

, 4 May 2005
Rate this:
Please Sign up or sign in to vote.
A simple way to turn those long, scraggly tooltips into a neat paragraph.


Don't you just hate it when your tooltips struggle across the face of your app, looking like they need a good haircut? Here's an easy way to turn them into multi-line tooltips. You can still write the tooltip directly into the Properties window of the control, and format them at runtime to any width that you want.


Most of the apps that I write include a subroutine that does various things to the controls, to enhance their appearance or behavior. This subroutine runs through all the controls in my app and adds a little nicety to them, depending on the type of control. This is the place where I format the tooltips.

The first thing to do is to decide on how wide a box you want your tooltips to occupy. Typographic guidelines tell us that the human eye does best when a line of characters is about 2.5 times as long as the alphabet (and the length of the alphabet depends on the typeface and the point size). Well, that's just too complicated. So I decided to make my tooltips not more than 78 characters wide, with an optimum width of 70-75. If a tooltip is shorter than 75 characters, I just leave it alone. But if it's longer, I run it through a short function to divide it into multiple lines. The function creates an array of words, each array element being one word of the tooltip. It then concatenates the words, checking the length as it adds each word, and inserts line breaks wherever the length of the concatenated string reaches my optimum width.


In my subroutine that enhances all controls, I check the length of the tooltip to decide if I need to split it into shorter lines:

Dim tt As String = ToolTip1.GetToolTip(ctrl
If Not tt = Nothing Then 
    If tt.Length > 75 Then ToolTip1.SetToolTip(ctrl, SplitToolTip(tt)) 
End If

Here's the function that splits up the long tooltips:

Friend Function SplitToolTip(ByVal strOrig As String) As String
Dim strArray As String() 
Dim SPACE As String = " " 
Dim CR As String = vbCrLf 
Dim strOneWord As String 
Dim strBuilder As String 
Dim strReturn As String 
    strArray = strOrig.Split(SPACE) 
    For Each strOneWord In strArray
        strBuilder = strBuilder & strOneWord & SPACE
        If Len(strBuilder) > 70 Then 
            strReturn = strReturn & strBuilder & CR 
            strBuilder = "" 
        End If 
    If Len(strBuilder) < 8 Then strReturn = strReturn.Substring(0, _
                                                strReturn.Length - 2)
    Return strReturn & strBuilder
End Function


The function is pretty straightforward. But I do want to mention the last If statement. I don't like it when just one word hangs onto a line by itself. So if I have just a little bit left after splitting things up, I tack the last few characters onto the end of the last full line. Yes, it might make it slightly longer, but to me that's better than one word dangling. Can you tell I used to be a typographer?

I used a For...Next loop because then I don't have to worry about how many elements are there in the array. If you are not familiar with this type of looping structure, check it out in the documentation. It's no more complicated than what you see here, and saves you the trouble of figuring out how many times to loop. Simply by establishing a variable that has the same data type as the array, you can loop through each element of the array.

Finally, note that because my variable CR is actually two characters (a carriage return and a line feed), I have to lop two characters off the end of strReturn if I want to correctly append the little bit of leftover text.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United States United States
Terpy is a consultant reluctantly based in Olympia, Wash. She'd much rather be back in Maine, where the ocean is on the correct side of the road. She is largely a self-taught programmer who clings jealously to her bad habits. Handbells, anyone?

You may also be interested in...


Comments and Discussions

QuestionThe absolute worst code piece I've ever seen!! Pin
steph012-Jul-11 4:13
membersteph012-Jul-11 4:13 
GeneralGreat snippit Pin
Roger James1-Dec-10 2:26
memberRoger James1-Dec-10 2:26 
Generalanother wordwrap function [modified] Pin
max_hodges15-Feb-07 1:22
membermax_hodges15-Feb-07 1:22 
GeneralCTRL not declared Pin
hkinser916-Dec-06 13:40
memberhkinser916-Dec-06 13:40 
GeneralRe: CTRL not declared Pin
terpy17-Dec-06 8:58
memberterpy17-Dec-06 8:58 
GeneralRe: CTRL not declared Pin
hkinser917-Dec-06 13:27
memberhkinser917-Dec-06 13:27 
GeneralSuggestion Pin
Karl_Shifflett10-May-05 13:43
memberKarl_Shifflett10-May-05 13:43 
GeneralRe: Suggestion Pin
BloodBaz20-Jun-05 3:22
memberBloodBaz20-Jun-05 3:22 
GeneralSubclassing ToolTip Pin
Pinx9-May-05 22:45
memberPinx9-May-05 22:45 
GeneralRe: Subclassing ToolTip Pin
terpy10-May-05 7:32
memberterpy10-May-05 7:32 
GeneralRe: Subclassing ToolTip Pin
Pinx19-Jun-05 23:00
memberPinx19-Jun-05 23:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 4 May 2005
Article Copyright 2005 by terpy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid