[英]Show ToolTip on DataGridView when cell value has changed
I want to show a ToolTip
when the value of a cell in a DataGridView
control is changed and the introduced value is invalid. 当
DataGridView
控件中单元格的值更改并且引入的值无效时,我想显示一个ToolTip
。
This can easily be done with the code below, but the problem is that after the ToolTip
is shown, it is also associated with the control, so every time the DataGridView
is hovered the ToolTip
is displayed, and I want it to be shown only once after the data has been changed. 可以使用下面的代码轻松完成此操作,但是问题是在
ToolTip
之后,它也与控件相关联,因此,每次将DataGridView
悬停时,都会显示该ToolTip
,并且我希望仅将其显示一次数据更改后。
I have noticed that this happens only with DataGridView
and GroupBox
. 我注意到,这仅在
DataGridView
和GroupBox
。 With a TextBox
or a Button
the ToolTip
is not associated with the control when shown over it. 使用
TextBox
或Button
, ToolTip
在控件上方时不与控件关联。
Why does this happen? 为什么会这样?
public partial class Form1 : Form
{
this.dataGridView1.ShowCellToolTips = false; // so we can show regular tooltip
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView control = (DataGridView)sender;
// check if control.Rows[e.RowIndex].Cells[e.ColumnIndex].Value is valid
if (invalid)
{
toolTip.Show("Invalid data", dataGridView1, 5000);
}
}
There are many ways to deal with this. 有很多方法可以解决这个问题。 The simplest and most direct seems to be to
Hide
the ToolTip
when you are leaving the DataGridView
: 最简单,最直接的方法似乎是在离开
DataGridView
时Hide
ToolTip
:
private void dataGridView1_Leave(object sender, EventArgs e)
{
toolTip1.Hide(this);
}
Of course it is up to you to decide upon the full design of what should happen while the error is still there..! 当然,完全由您决定在错误仍然存在的情况下应该如何处理的完整设计。
As for Textboxes
: They are very special and there is usually no use in asking why they behave diffently.. 至于
Textboxes
:它们非常特殊,通常无用问为什么它们表现不同。
Another way would be to code the Popup event and use the Tag as a flag to make sure it is shown only once: 另一种方法是对Popup事件进行编码,并使用Tag作为标志以确保仅显示一次:
Before you show it in the CellValueChanged you set a flag: 在将其显示在CellValueChanged中之前,请设置一个标志:
toolTip1.Tag = "true";
and in the Popup event you check for it: 然后在Popup事件中检查它:
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (toolTip1.Tag == null) return;
bool show = toolTip1.Tag.ToString() == "true";
if (toolTip1.Tag != "true") e.Cancel = true;
toolTip1.Tag = "false";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.