jello
jello

Reputation: 745

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?

Upvotes: 5

Views: 7830

Answers (7)

Joe Ferry
Joe Ferry

Reputation: 21

If you enter a tool tip in the DataGridView properties control for columns, and I suspect other WinForms control designers, and include a "\r\n" in the tool tip, you will see "\\r\\n" in the tool tip that is displayed when you run the code. This is because the tool tip property is already a string, and the designer interprets the code from the tool tip entered in the properties window as "\\r\\n". You can go to the .Designer.cs file, manually edit the file, and replace "\\r\\n" with "\r\n". You will then see the tool tip with the line break when the application runs. If you go back and look at the value of the tool tip in the designer, you will see the tool tip without the line break, but it will be there, and will not be re-transformed to "\\r\\n" by the designer. Despite a comment made earlier, you only need the "\n". The "\r" is not necessary.

Upvotes: 0

Demodave
Demodave

Reputation: 6662

Using new line worked for me

System.Environment.NewLine

Upvotes: 0

iankb
iankb

Reputation: 91

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.

Upvotes: 7

TULRangerFan
TULRangerFan

Reputation: 21

You can't add the \r\n in the tooltip property of the control. It simply doesn't work. To resolve you issue, simply add the tooltip in the code itself typically in the InitializeComponent method. I.E.: (c#, winform example)

this.mToolTip.SetToolTip(this.tbxControl,
    "This is the first line of the tooltip.\r\n" +
    "This is the second line of the tooltip.");

Upvotes: 2

Jeffrey L Whitledge
Jeffrey L Whitledge

Reputation: 59553

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();
}

Upvotes: 6

Jay
Jay

Reputation: 57979

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);

Upvotes: 9

Justin Ethier
Justin Ethier

Reputation: 134275

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

Upvotes: 3

Related Questions