winforms大段工具提示

jel*_*llo 4 c# tooltip winforms

当我将鼠标悬停在某个图片框上时,我正试图在工具提示中显示一个段落.问题是,工具提示采用该段落,并在整个屏幕上跨越一行.我怎样才能使它占用更小,更易读的区域?或者,也许你有另一种技术来实现同样的东西,但没有工具提示功能?

Jay*_*Jay 9

在字符串中添加换行符.

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


Jef*_*dge 6

这是你可以使用的东西:

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();
}
Run Code Online (Sandbox Code Playgroud)


小智 6

您不需要使用代码来包含\ r \n字符.

如果单击ToolTip属性值右侧的下拉箭头,则会显示多行编辑框.

只需按Enter即可创建新行.


Jus*_*ier 3

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

  • Windows 上的换行符不是 \n,而是 \r\n,并且您应该使用 Environment.NewLine。但是,是的,几乎只需在字符串中添加换行符即可。 (4认同)