views:

188

answers:

3

I'm trying to display a paragraph in a tooltip when I hover a certain picture box. The problem is, the tooltip takes that paragraph, and spans it on one line, all across my screen. How can I make it so that it takes a smaller, more readable area? or, maybe you have another technique to achieve the same thing, but without the tooltip function?

+3  A: 

Have you tried inserting newlines \n into the tooltip, to get it to span multiple lines?

Justin Ethier
Newlines are not \n on Windows, they are \r\n, and you should be using Environment.NewLine. But yeah, pretty much, just add newlines to the string.
Ed Swangren
hmmmm indeed that works, thx. but is there another simpler way? because now I have to make a function to go through the paragraph, and add newlines every couple of words
jello
\n works by the way
jello
hmmm looks like i have to write a function to add a new line every couple of words. also, do u guys know what property to change for the tooltip to not fade out after a few seconds?
jello
See: http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.autopopdelay.aspx - `AutoPopDelay` controls the period of time, in milliseconds, that the ToolTip remains visible when the pointer is stationary on a control. So if you set this to a very large number that ought to do the trick.
Justin Ethier
no, AutoPopDelay doesn't do it. I put 999999, and it disappears after 15 seconds...
jello
by the way, this does not work for vb.NET
serhio
@jello: you should set it to "Infinite".
serhio
+6  A: 

Add line breaks to your string.

string tooltip = string.Format("Here is a lot of text that I want{0}to display on multiple{0}lines.", Environment.NewLine);
Jay
+1  A: 

Here's something you can use:

private const int maximumSingleLineTooltipLength = 20;

private static string AddNewLinesForTooltip(string text)
{
    if (text.Length < maximumSingleLineTooltipLength)
        return text;
    int lineLength = (int)Math.Sqrt((double)text.Length) * 2;
    StringBuilder sb = new StringBuilder();
    int currentLinePosition = 0;
    for (int textIndex = 0; textIndex < text.Length; textIndex++)
    {
        // If we have reached the target line length and the next 
        // character is whitespace then begin a new line.
        if (currentLinePosition >= lineLength && 
              char.IsWhiteSpace(text[textIndex]))
        {
            sb.Append(Environment.NewLine);
            currentLinePosition = 0;
        }
        // If we have just started a new line, skip all the whitespace.
        if (currentLinePosition == 0)
            while (textIndex < text.Length && char.IsWhiteSpace(text[textIndex]))
                textIndex++;
        // Append the next character.
        if (textIndex < text.Length)
            sb.Append(text[textIndex]);

        currentLinePosition++;
    }
    return sb.ToString();
}
Jeffrey L Whitledge
thx a lot for the code, but I won't use tooltips until I figure out how to not make it go away after a few seconds
jello