簡體   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