MultiLine ToolTips in designer- An Answer

B

bullshark

VS2003

1) If you don't have one, add a tooltip control to your design canvas.
2) open the properties for the control that gets the tool tip
3) type in all your text in the 'tooltip on tooltip1' property.
The line feeds come later. The property editor has no way to enter
a \n. It doesn't parse and it doesn't accept control characters
escaped or not.
4) Switch from the design view to the form code.
e.g. 'MainFrm.cs' instead of 'MainFrm.cs[Design]'
5) Find the code that creates the control. It will be in the
InitializeComponent() method that you are NOT supposed to edit.
(More about that later)
6) Change the code for the tooltip from:
this.toolTip1.SetToolTip(this.someControl, "Line1Line2");
to:
this.toolTip1.SetToolTip(this.someControl, "Line1\nLine2");
7) Build and run. When the cursor floats over the control, you'll have 2 lines.
Do NOT look at the design page or examine properties.
8) Exit the solution (close it or save it, or open another, or exit VS)
9) Reopen the solution. Examine the properties for the control.
Tooltip on Tooltip 1 will say: "Line1[]Line2", AND
the linefeed will *stay* until you remove it, even if you edit/change
the design or properties.

Not only that, but the non-printable character '[]' that represents the line
feed in the tool tip text will copy and paste.

Explanation:

The InitializeComponent method is in a reserved region like this:
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

....

#endregion

Whenever you are on the design page making changes, that region is
'empty'. Anytime you look at the page that has the code, or compile the
form, the design page generates code representing the current state
of the design and stuffs it in the region.

When and how are not material. The point is that the region is volatile
and will be re-written anytime the design view wants to (changes).

Now, I'm guessing, but it seems that when put away, the *code* is the
persistent backing for the design view of the form. So when you put
away the solution, and reload it, the design view is recreated from
the code. As a result, the tooltip on tooltip 1 text property now
has an embedded line feed character. The trick here, is that by changing
the 'region code' you have changed the persistent image of the form
without the knowledge of the design view.

This is a dirty trick, and I don't know how long it will last, but
it sure beats a stick in the eye. In the meantime, I have a copy/
pastable linefeed character that I can use in any of the properties that
accept text.

regards

bullshark
 
E

Eric Cadwell

Sweet! I had tried using \n in the tooltip to no avail, but was able to set
them at runtime. That upset me and I had to shorten many of my tooltips so
they wouldn't trail across the screen.

Nice job.

-Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top