Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

winforms big paragraph tooltip

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?

like image 832
jello Avatar asked Mar 25 '10 02:03

jello


3 Answers

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);
like image 62
Jay Avatar answered Nov 17 '22 05:11

Jay


You don't need to use code to include \r\n characters.

If you click on the dropdown arrow to the right of the ToolTip property value, it displays a multiline edit box.

Just press Enter to create a new line.

like image 7
iankb Avatar answered Nov 17 '22 07:11

iankb


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();
}
like image 6
Jeffrey L Whitledge Avatar answered Nov 17 '22 05:11

Jeffrey L Whitledge