繁体   English   中英

winforms大段工具提示

[英]winforms big paragraph tooltip

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

在字符串中添加换行符。

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

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

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

只需按Enter即可创建新行。

这是你可以使用的东西:

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

您无法在控件的tooltip属性中添加\\r\\n 它根本不起作用。 要解决您的问题,只需在代码本身中添加工具提示,通常在InitializeComponent方法中。 IE :( c#,winform示例)

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

如果您在DataGridView属性控件中为列输入工具提示,并且我怀疑其他WinForms控件设计器,并在工具提示中包含“\\ r \\ n”,您将在工具中看到“\\\\ r \\\\ n”运行代码时显示的提示。 这是因为工具提示属性已经是一个字符串,设计人员将在属性窗口中输入的工具提示中的代码解释为“\\\\ r \\\\ n”。 您可以转到.Designer.cs文件,手动编辑该文件,并将“\\\\ r \\\\ n”替换为“\\ r \\ n”。 然后,当应用程序运行时,您将看到带有换行符的工具提示。 如果您回过头来查看设计器中工具提示的值,您将看到没有换行符的工具提示,但它会在那里,并且不会被重新转换为“\\\\ r \\\\ n”由设计师。 尽管先前做了评论,但您只需要“\\ n”。 “\\ r”不是必需的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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