Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: winforms: tooltip: how to delay tooltip.Show

I'm using winforms ToolTip class on a DataGridView (not tooltip of datagridview since I need custom formatting.)

And I call toolTip.Show() in DataGridView's CellMouseEnter event.

toolTip.Show() shows the the tooltip immediately and InitialDelay property is not working since I called toolTip.Show().

Is there another way to delay toolTip.Show() just like regular initaldelay.

like image 352
Osman Karatemiz Avatar asked Dec 09 '25 14:12

Osman Karatemiz


1 Answers

ToolTip Show method shows right away the tooltip text, if you want a delay you have to use SetToolTip instead, being 5000 milliseconds the maximun:

toolTip.InitialDelay = 5000;
toolTip.SetToolTip(dataGridView1, "Max InitialDelay is 5000 milliseconds");  

Note: for the above to work properly, remember you first have to disable DataGridView builtin tooltip :

dataGridView1.ShowCellToolTips = false;


EDIT: To show tool tip for each row (and cell). Note the use of CellMouseEnter and CellMouseLeave events

private ToolTip toolTip;

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex == -1 || e.ColumnIndex == -1) return;
    var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

    if (cell.Value != null){
        toolTip = new ToolTip();
        toolTip.InitialDelay = 3000;
        dataGridView1.ShowCellToolTips = false;
        toolTip.SetToolTip(dataGridView1, cell.Value.ToString());
    }    
}

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
     if (toolTip != null)
         toolTip.Dispose();
}
like image 82
E-Bat Avatar answered Dec 11 '25 06:12

E-Bat