显示时如何刷新DataGridView单元格的工具提示? [英] How to refresh tooltip of a DataGridView cell while it's shown?
问题描述
我试图刷新 DataGridViewCell
的工具提示,而无需离开并使用光标重新输入单元格.
I am trying to refresh the tooltip of a DataGridViewCell
without leaving and reentering the cell with the cursor.
我为单元格的 ToolTipText
属性分配了一个新值,但是在显示工具提示时,更改列的 ToolTipText
属性对工具提示没有任何影响正在显示.
I assign a new value to ToolTipText
property of the cell, but while the tooltip is showing, changing ToolTipText
property of column doesn't have any impact on the tooltip which is showing.
这是我的服务器应用程序的快照.人们可以加入,并且您可以看到他们的ping:
This is a snapshot of a server application of mine. People can join and you can see their ping:
我希望能够看到ping的变化.
I want to be able to see how the ping changes.
推荐答案
当显示单元格的 ToolTip
时,更改单元格的 ToolTipText
不变工具提示文字会自动显示.要对其进行更改,可以处理 DataGridView
的 CellToolTipChanged
事件,以检测单元格的 ToolTipText
中的更改.然后,您可以使用以下代码检查使 DataGridView
在工具提示中显示新文本:
When the ToolTip
of a cell is showing, changing the ToolTipText
of the cell doesn't change the tool tip text automatically. To change it, you can handle CellToolTipChanged
event of DataGridView
to detect a change in ToolTipText
of a cell. Then you can check make the DataGridView
shows the new text in tooltip using such code:
private void dgv_CellToolTipTextChanged(object sender, DataGridViewCellEventArgs e)
{
var grid = (DataGridView)sender;
var toolTipControl = grid.GetType().GetField("toolTipControl",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance).GetValue(grid);
var activated = (bool)toolTipControl.GetType()
.GetProperty("Activated").GetValue(toolTipControl);
if (activated)
{
var cell = grid[e.ColumnIndex, e.RowIndex];
var ActivateToolTip = typeof(DataGridView).GetMethod("ActivateToolTip",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
ActivateToolTip.Invoke(grid,
new object[] { true, cell.ToolTipText, e.ColumnIndex, e.RowIndex });
}
}
示例
作为示例,我在计时器中更改了单元格的 ToolTipText
以获得以下结果:
As an example, I changed the ToolTipText
of a cell in a timer to get the following result:
private void timer1_Tick(object sender, EventArgs e)
{
this.dataGridView1.Rows[0].Cells[0].ToolTipText = DateTime.Now.ToString();
}
这篇关于显示时如何刷新DataGridView单元格的工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!