jel*_*llo 4 c# tooltip winforms
当我将鼠标悬停在某个图片框上时,我正试图在工具提示中显示一个段落.问题是,工具提示采用该段落,并在整个屏幕上跨越一行.我怎样才能使它占用更小,更易读的区域?或者,也许你有另一种技术来实现同样的东西,但没有工具提示功能?
在字符串中添加换行符.
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)
这是你可以使用的东西:
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)
您是否尝试过\n在工具提示中插入换行符,以使其跨越多行?