简体   繁体   English

winforms大段工具提示

[英]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. 您不需要使用代码来包含\\ r \\ n字符。

If you click on the dropdown arrow to the right of the ToolTip property value, it displays a multiline edit box. 如果单击ToolTip属性值右侧的下拉箭头,则会显示多行编辑框。

Just press Enter to create a new line. 只需按Enter即可创建新行。

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. 您无法在控件的tooltip属性中添加\\r\\n It simply doesn't work. 它根本不起作用。 To resolve you issue, simply add the tooltip in the code itself typically in the InitializeComponent method. 要解决您的问题,只需在代码本身中添加工具提示,通常在InitializeComponent方法中。 IE: (c#, winform example) 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

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. 如果您在DataGridView属性控件中为列输入工具提示,并且我怀疑其他WinForms控件设计器,并在工具提示中包含“\\ r \\ n”,您将在工具中看到“\\\\ r \\\\ n”运行代码时显示的提示。 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". 这是因为工具提示属性已经是一个字符串,设计人员将在属性窗口中输入的工具提示中的代码解释为“\\\\ r \\\\ n”。 You can go to the .Designer.cs file, manually edit the file, and replace "\\\\r\\\\n" with "\\r\\n". 您可以转到.Designer.cs文件,手动编辑该文件,并将“\\\\ r \\\\ n”替换为“\\ 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. 如果您回过头来查看设计器中工具提示的值,您将看到没有换行符的工具提示,但它会在那里,并且不会被重新转换为“\\\\ r \\\\ n”由设计师。 Despite a comment made earlier, you only need the "\\n". 尽管先前做了评论,但您只需要“\\ n”。 The "\\r" is not necessary. “\\ r”不是必需的。

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

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