27

How can I programatically cause a control's tooltip to show in a Winforms app without needing the mouse to hover over the control? (P/Invoke is ok if necessary).

7 Answers 7

28

If you are using the Tooltip control on the form, you can do it like this:

ToolTip1.Show("Text to display", Control)

The MSDN documentation for the ToolTip control's "Show" method has all the different variations on this and how to use them.

4
  • 6
    I don't think this is quite right. If you do that then the tooltip will show but it will also show everytime you move the mouse over the control there after. Isn't there a way to just show the tooltip on command one time? Oct 12, 2011 at 14:13
  • 1
    No, using the "Show" command shows the tooltip only that one time. To make the tooltip show any time the mouse is over the control, you'd set the "ToolTipText" property of the control itself.
    – Keithius
    Oct 14, 2011 at 19:35
  • 1
    Jordan S is right. Show method sets the ToolTip text associated with the specified control. Apr 22, 2012 at 15:31
  • Confirming @PICyourBrain's behaviour, though I don't think that this should be the case.
    – SteveCinq
    Aug 7, 2022 at 22:53
10
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip(this.textBox1, "Hello");

The tooltip will be set over the control "textBox1".

Have a read here:

http://msdn.microsoft.com/en-us/library/aa288412.aspx

1

First You need to add tooltip control to the form Second attach the tooltip control to some control you want the tooltip to show on (MyControl) Third do this: Tooltip1.Show("My ToolTip Text", MyControl)

1

Kevin, if you want to create your own balloon, read this link:Task 3: Showing Balloon tips. There mentioned NativeMethods class with the TOOLTIPS_CLASS constant.

0

This is the code I use:

static HWND hwndToolTip = NULL;

void CreateToolTip( HWND hWndControl, TCHAR *tipText )
{  
    BOOL success;

  if( hwndToolTip == NULL )
  {
    hwndToolTip = CreateWindow(  TOOLTIPS_CLASS, 
                                 NULL, 
                                 WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,                                     
                                 CW_USEDEFAULT, CW_USEDEFAULT, 
                                 CW_USEDEFAULT, CW_USEDEFAULT,                                     
                                 NULL, NULL,
                                 hInstResource, 
                                 NULL ); 
  }

  if( hwndToolTip )
  { 
    TOOLINFO ti; 

    ti.cbSize   = sizeof(ti); 
    ti.uFlags   = TTF_TRANSPARENT | TTF_SUBCLASS; 
    ti.hwnd     = hWndControl; 
    ti.uId      = 0; 
    ti.hinst    = NULL; 
    ti.lpszText = tipText; 

    GetClientRect( hWndControl, &ti.rect ); 

    success = SendMessage( hwndToolTip, TTM_ADDTOOL, 0, (LPARAM) &ti ); 
  }
}

Call CreateToolTip function to create a tool tip for a certain control.

1
  • How to get reference to TOOLTIPS_CLASS, I get a compiler error for HWND, TCHAR and many more. These don't seem to be defined in .Net. Do I have to add a reference?
    – Kevin Boyd
    Jun 19, 2012 at 14:56
0

If you create your variable private to the whole form, you will be able to call the sub for the and adjust the initialdelay.

Public Class MyForm        
Private MyTooltip As New ToolTip        
...        
Sub ApplyToolTips        
'For default        
ApplyToolTips (1000)        
End Sub        

Sub ApplyTooltips (ByVal Delay as Integer)        

MyTooltip .InitialDelay = Delay        
MyTooltip.AutoPopDelay = 5000        
...        
MyTooltip.SetToolTip(Me.btnClose, "Close the form")        

End Sub       

Private Sub Btn_Click(sender As System.Object, e As System.EventArgs) Handles Btn.Click           
    Dim PicBox As PictureBox = CType(sender, PictureBox)        
    ApplyTooltips (0)       
    ApplyTooltips (1000)       
End Sub       
0

After trying @Keithius's code and finding that the tip showed with mouse-over once the OnClick code had run, I ended up doing this:

Button Click event:

ToolTip1.Show("Text to display", Control);

Button MouseLeave event:

ToolTip1.Hide(Control);

But then I found that if I specified a position - using either explicit x and y co-ords or a Point, it worked as expected:

Button Click event:

ToolTip1.Show("Text to display", Control, Control.Width+20, 0, 2000);

or

ToolTip1.Show("Text to display", Control, new Point(Control.Width+20, 0), 2000);

This is possibly due to the modality of some overloads as mentioned in the Microsoft doco.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.