Windows Develop Bookmark and Share   
 index > Windows Forms General > setting a tooltip to a rectangle or region
 

setting a tooltip to a rectangle or region

Hello,
I have drawn a rectangle, and would like to set a tooltip to this rectangle. For instance:

Code Snippet
Rectangle rect1 = new Rectangle(10, 70, 80, 40);
toolTip1.SetToolTip(rect1, "Please add this tool tip to my rectangle.");

However, I receive two errors with this:
1. The best overloaded method match for 'System.Windows.Forms.ToolTips.SetToolTip
(System.Windows.Forms.Control, string)' has some invalid arguments

2. Argument '1': cannot convert from 'System.Drawing.Rectangle' to
'System.Windows.Forms.Control'

So, is there another way to set a tooltip to a rectangle, or is there a way to convert a rectangle to form control?
Thanks,
Joe

joebro  Thursday, February 21, 2008 10:20 PM

You might try placing a Panel control on the Form and setting the BorderStyle of that Panel control.

You will be able to set the ToolTip associated with Panel.

Code Snippet

Panel p = new Panel();

p.Bounds = new Rectangle(10, 70, 80, 40);

p.BorderStyle = BorderStyle.FixedSingle;

this.Controls.Add(p);

toolTip1.SetToolTip(p, "Please add this tool tip to my rectangle.");

Instead of using a Control, you could also manually display the tooltip at the proper location using the ToolTip.Show method. The following is a very rough example. This is a lot trickier to get right than using the Panel control.

Code Snippet

private bool _InRectangle;

private void Form1_MouseMove(object sender, MouseEventArgs e)

{

Rectangle r = new Rectangle(10, 70, 80, 40);

if (r.Contains(e.Location))

{

if (!_InRectangle)

{

_InRectangle = true;

toolTip1.Show("Testing 1,2,3", this, e.Location);

}

}

else

{

if (_InRectangle)

{

_InRectangle = false;

toolTip1.Hide(this);

}

}

}

BinaryCoder  Thursday, February 21, 2008 11:50 PM

You might try placing a Panel control on the Form and setting the BorderStyle of that Panel control.

You will be able to set the ToolTip associated with Panel.

Code Snippet

Panel p = new Panel();

p.Bounds = new Rectangle(10, 70, 80, 40);

p.BorderStyle = BorderStyle.FixedSingle;

this.Controls.Add(p);

toolTip1.SetToolTip(p, "Please add this tool tip to my rectangle.");

Instead of using a Control, you could also manually display the tooltip at the proper location using the ToolTip.Show method. The following is a very rough example. This is a lot trickier to get right than using the Panel control.

Code Snippet

private bool _InRectangle;

private void Form1_MouseMove(object sender, MouseEventArgs e)

{

Rectangle r = new Rectangle(10, 70, 80, 40);

if (r.Contains(e.Location))

{

if (!_InRectangle)

{

_InRectangle = true;

toolTip1.Show("Testing 1,2,3", this, e.Location);

}

}

else

{

if (_InRectangle)

{

_InRectangle = false;

toolTip1.Hide(this);

}

}

}

BinaryCoder  Thursday, February 21, 2008 11:50 PM
Thank you BinaryCoder! This works very nicely.

joebro  Friday, February 22, 2008 12:31 AM

You can use google to search for other answers

Custom Search

More Threads

• associate ContextMenuStrip toToolStripMenuItem
• Read DataTable from another form
• How to SET FOCUS on multiple textBoxes?
• Background image on MenuStrip separators in .NET 2.0
• Is there a standard Vc++ api to write excel or text file from dataset?
• How to localize the two tooltip strings: "Categorized" & "Alphabetical"? System.Windows.Forms.dll?
• #develop
• Problem With CheckedListBox
• Check If sql Server Exits
• How to make a text file from a rich text box???