簡體   English   中英

選擇 datagridview 單元格時顯示工具提示

[英]Show tooltip when datagridview cell is selected

如何在選擇單元格時顯示 datagridview 的工具提示,而不是從鼠標懸停而是使用箭頭鍵?

正如您所注意到的,您將無法使用 DataGridView 的內置工具提示。 事實上,您需要禁用它,因此將 DataGridView 的ShowCellToolTips屬性設置為false (默認情況下為true )。

您可以將 DataGridView 的CellEnter事件與常規 Winform ToolTip 控件一起使用,以在焦點從一個單元格更改為另一個單元格時顯示工具提示,無論這是使用鼠標還是箭頭鍵完成的。

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) {
    var cell = dataGridView1.CurrentCell;
    var cellDisplayRect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
    toolTip1.Show(string.Format("this is cell {0},{1}", e.ColumnIndex, e.RowIndex), 
                  dataGridView1,
                  cellDisplayRect.X + cell.Size.Width / 2,
                  cellDisplayRect.Y + cell.Size.Height / 2,
                  2000);
    dataGridView1.ShowCellToolTips = false;
}

請注意,我根據單元格的高度和寬度為 ToolTip 的位置添加了偏移量。 我這樣做是為了讓工具提示不會直接出現在單元格上; 您可能需要調整此設置。

Jay Riggs 的答案是我使用的答案。 另外,因為我需要更長的持續時間,所以我不得不添加這個事件以使工具提示消失。

private void dataGridView_MouseLeave(object sender, EventArgs e)
{
    toolTip1.Hide(this);
}
        dgv.CurrentCellChanged += new EventHandler(dgv_CurrentCellChanged);
    }

    void dgv_CurrentCellChanged(object sender, EventArgs e)
    {
        // Find cell and show tooltip.
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM