tags:

views:

1104

answers:

4

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).

A: 

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.

Mark D
+1  A: 
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

Mark Ingram
+4  A: 

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.

Keithius
A: 

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)

John