tags:

views:

3631

answers:

7

How can I display a tooltip over a button using Winforms?

A: 

Sure, just handle the mousehover event and tell it to display a tool tip. t is a tooltip defined either in the globals or in the constructor using:

ToolTip t = new ToolTip();

then the event handler:

private void control_MouseHover(object sender, EventArgs e)
{
  t.Show("Text", (Control)sender);
}
Fry
I think you got voted down because that's not the way to use ToolTip controls in Windows Forms. You only need one such control on the form and it shows the tips for all the controls. See code in the other responses.
julianz
I guess the explanation doesn't match the code, Where in the explanation I said do display it and in the code I initialized it as well. My bad. :P
Fry
@ julianz Actually, this works well for having specialized tooltips which can be dynamic if you want depending on state (minus of course the creation - forgive me, I was just trying to fit it all into one block.) As for other responses on a similar vein... yshuditelu and Dylan Beattie were similar albeit without the instantiation.
Fry
+1  A: 

You can use the ToolTip class:

Creating a ToolTip for a Control

Example:

private void Form1_Load(object sender, System.EventArgs e)
{
    System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
    ToolTip1.SetToolTip(this.Button1, "Hello");
}
DaveK
A: 

The .NET framework provides a ToolTip class. Add one of those to your form and then on the MouseHover event for each item you would like a tooltip for, do something like the following:

    private void checkBox1_MouseHover(object sender, EventArgs e)
    {
        toolTip1.Show("text", checkBox1);
    }
Timothy Carter
+7  A: 

The ToolTip is actually a WinForms control that handles displaying tool tips for multiple elements on a single form.

Say your button is called MyButton.

  1. Add a ToolTip control (under Common Controls in the Windows Forms toolbox) to your form.
  2. Give it a name - say MyToolTip
  3. Set the "Tooltip on MyToolTip" property of MyButton (under Misc in the button property grid) to the text that should appear when you hover over it.

The tooltip will automatically appear when the cursor hovers over the button, but if you need to display it programatically, call

MyToolTip.Show("Tooltip text goes here", MyButton)

in your code to show the tooltip, and MyToolTip.Hide(MyButton) to make it disappear again.

Dylan Beattie
+1  A: 

Using the form designer:

Drag the ToolTip control from the Toolbox, onto the form. Select the properties of the control you want the tool tip to appear on. Find the property 'ToolTip on toolTip1' (the name may not be toolTip1 if you changed it's default name). Set the text of the property to the tool tip text you would like to display.

You can set also the tool tip programatically using the following call...

this.toolTip1.SetToolTip(this.targetControl, "My Tool Tip");

jmatthias
A: 

thanks a lot,its very usefuLL!!

A: 

Set the ShowAlways property to true on the ToolTip

MC