简体   繁体   中英

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?

在字符串中添加换行符。

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

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.

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

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. IE: (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.");

您是否尝试在工具提示中插入换行符\\n ,以使其跨越多行?

使用新线路为我工作

System.Environment.NewLine

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM