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?
+3
A:
Have you tried inserting newlines \n
into the tooltip, to get it to span multiple lines?
Justin Ethier
2010-03-25 02:40:39
Newlines are not \n on Windows, they are \r\n, and you should be using Environment.NewLine. But yeah, pretty much, just add newlines to the string.
Ed Swangren
2010-03-25 02:44:11
hmmmm indeed that works, thx. but is there another simpler way? because now I have to make a function to go through the paragraph, and add newlines every couple of words
jello
2010-03-25 02:51:50
\n works by the way
jello
2010-03-25 03:02:47
hmmm looks like i have to write a function to add a new line every couple of words. also, do u guys know what property to change for the tooltip to not fade out after a few seconds?
jello
2010-03-25 03:19:48
See: http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.autopopdelay.aspx - `AutoPopDelay` controls the period of time, in milliseconds, that the ToolTip remains visible when the pointer is stationary on a control. So if you set this to a very large number that ought to do the trick.
Justin Ethier
2010-03-25 11:42:59
no, AutoPopDelay doesn't do it. I put 999999, and it disappears after 15 seconds...
jello
2010-03-27 05:00:30
by the way, this does not work for vb.NET
serhio
2010-09-09 09:39:58
@jello: you should set it to "Infinite".
serhio
2010-09-09 09:41:02
+6
A:
Add line breaks to your string.
string tooltip = string.Format("Here is a lot of text that I want{0}to display on multiple{0}lines.", Environment.NewLine);
Jay
2010-03-25 02:41:54
+1
A:
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();
}
Jeffrey L Whitledge
2010-03-25 03:24:18
thx a lot for the code, but I won't use tooltips until I figure out how to not make it go away after a few seconds
jello
2010-03-25 03:28:42