Winforms large paragraph tooltip

I am trying to display a paragraph in a tooltip when I find a specific box for an image. The problem is that the tooltip accepts this paragraph and covers it on one line, across the screen. How can I make it so that it occupies a smaller, more readable area? or maybe you have another technique to achieve the same, but without a tooltip function?

+4
source share
7 answers

Have you tried inserting new lines \n in a tooltip to span multiple lines?

+2
source

Add the lines to the line.

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

Here 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(); } 
+6
source

You do not need to use code to include the characters \ r \ n.

If you click on the drop-down arrow to the right of the value of the ToolTip property, it will display a multi-line edit field.

Just hit Enter to create a new line.

+6
source

You cannot add \r\n to the tooltip property of a control. It just doesn't work. To solve the problem, just add a tooltip in the code itself, usually in the InitializeComponent method. IE: (C # winform example)

 this.mToolTip.SetToolTip(this.tbxControl, "This is the first line of the tooltip.\r\n" + "This is the second line of the tooltip."); 
+2
source

Using a new line worked for me

 System.Environment.NewLine 
0
source

If you enter a tooltip in a DataGridView property control for columns, and I suspect that other WinForms control constructors will add "\ r \ n" to the tooltip, you will see "\\ r \\ n" in the tool. The tooltip that appears when you run the code. This is because the tooltip property is already a string, and the designer interprets the code from the tooltip entered in the properties window as "\\ r \\ n". You can go to the file.Designer.cs, edit the file manually and replace "\\ r \\ n" with "\ r \ n". After that, you will see a hint with line breaks when starting the application. If you go back and look at the value of the tooltip in the constructor, you will see the tooltip without breaking the line, but it will be there and will not be converted to "\\ r \\ n" by the designer. Despite the comment made earlier, you only need "\ n". "\ R" is not necessary.

0
source

Source: https://habr.com/ru/post/1305056/


All Articles