简体   繁体   English

c#:winforms:tooltip:如何延迟tooltip.Show

[英]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.) 我在DataGridView上使用winforms ToolTip类(不是datagridview的工具提示,因为我需要自定义格式。)

And I call toolTip.Show() in DataGridView's CellMouseEnter event. 我在DataGridView的CellMouseEnter事件中调用toolTip.Show()。

toolTip.Show() shows the the tooltip immediately and InitialDelay property is not working since I called toolTip.Show(). toolTip.Show()立即显示工具提示,并且自从我调用toolTip.Show()以来,InitialDelay属性不起作用。

Is there another way to delay toolTip.Show() just like regular initaldelay. 有没有其他方法可以像普通的initaldelay一样延迟toolTip.Show()。

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: 工具提示显示方法立即显示工具提示文本,如果您想要延迟,则必须使用SetToolTip ,最大值为5000毫秒:

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 : 注意:为了使上述工作正常,请记住首先必须禁用DataGridView内置工具提示:

dataGridView1.ShowCellToolTips = false;


EDIT : To show tool tip for each row (and cell). 编辑 :显示每行(和单元格)的工具提示。 Note the use of CellMouseEnter and CellMouseLeave events 请注意CellMouseEnterCellMouseLeave事件的使用

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();
}

I have solved my problem; 我解决了我的问题; I created a new instance of ToolTip in DataGridView CellMouseEnter event and disposed it in CellMouseLeave event. 我在DataGridView CellMouseEnter事件中创建了一个新的ToolTip实例,并将其放置在CellMouseLeave事件中。

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        // 
        // create and initilize toolTip1
        // 
        this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
        this.toolTip1.OwnerDraw = true;
        this.toolTip1.UseAnimation = false;
        this.toolTip1.UseFading = false;
        this.toolTip1.Draw += new System.Windows.Forms.DrawToolTipEventHandler(this.toolTip1_Draw);
        this.toolTip1.Popup += new System.Windows.Forms.PopupEventHandler(this.toolTip1_Popup);

        toolTip1.SetToolTip((DataGridView)sender, e.RowIndex.ToString() + " : " + e.ColumnIndex.ToString());
    }
}

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        toolTip1.Dispose();
    }
}

But I m not sure if it is a good practice. 但我不确定这是不是一个好习惯。 I think it is not good for performance. 我认为这对表现不利。 because I can see it when scrolling datagridview up and down with mouse wheel, some rows seem like frozen for a moment until wheel stops. 因为当用鼠标滚轮向上和向下滚动数据网格视图时我可以看到它,有些行似乎冻结片刻直到轮子停止。

if there is a better solution please advice. 如果有更好的解决方案请咨询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM