当鼠标悬停在禁用的控件上时,我试图显示工具提示。由于禁用的控件无法处理任何事件,因此我必须以父级形式进行操作。我选择通过处理父表单中的MouseMove
事件来执行此操作。这是完成工作的代码:
void Form1_MouseMove(object sender, MouseEventArgs e)
{
m_toolTips.SetToolTip(this, "testing tooltip on " + DateTime.Now.ToString());
string tipText = this.m_toolTips.GetToolTip(this);
if ((tipText != null) && (tipText.Length > 0))
{
Point clientLoc = this.PointToClient(Cursor.Position);
Control child = this.GetChildAtPoint(clientLoc);
if (child != null && child.Enabled == false)
{
m_toolTips.ToolTipTitle = "MouseHover On Disabled Control";
m_toolTips.Show(tipText, this, 10000);
}
else
{
m_toolTips.ToolTipTitle = "MouseHover Triggerd";
m_toolTips.Show(tipText, this, 3000);
}
}
}
该代码确实处理了禁用控件的工具提示显示。问题是,当鼠标悬停在禁用的控件上时,工具提示将保持关闭状态并再次重新显示。从我在工具提示中添加的显示时间开始,当鼠标位于父窗体上方时,
MouseMove
事件大约每3秒调用一次,因此工具提示每3秒更新一次。但是,当鼠标悬停在禁用的控件上时,工具提示每1秒钟刷新一次。同样,当工具提示在表格上方刷新时,只有简短的文本会更新文本。但是,当工具提示刷新到禁用的控件上方时,工具提示窗口将关闭,就像鼠标移到启用的控件中一样,并且应该关闭工具提示。但是工具提示会立即重新出现。有人可以告诉我为什么吗?谢谢。
最佳答案
您只能在鼠标点击已失效的控件时显示一次工具提示,然后在鼠标离开时将其隐藏。请看下面的代码,它应该显示表单上所有禁用控件的工具提示消息
private ToolTip _toolTip = new ToolTip();
private Control _currentToolTipControl = null;
public Form1()
{
InitializeComponent();
_toolTip.SetToolTip(this.button1, "My button1");
_toolTip.SetToolTip(this.button2, "My button2");
_toolTip.SetToolTip(this.textBox1, "My text box");
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Control control = GetChildAtPoint(e.Location);
if (control != null)
{
if (!control.Enabled && _currentToolTipControl == null)
{
string toolTipString = _toolTip.GetToolTip(control);
// trigger the tooltip with no delay and some basic positioning just to give you an idea
_toolTip.Show(toolTipString, control, control.Width/2, control.Height/2);
_currentToolTipControl = control;
}
}
else
{
if (_currentToolTipControl != null) _toolTip.Hide(_currentToolTipControl);
_currentToolTipControl = null;
}
}
希望这会有所帮助,问候
关于c# - 在禁用的控件上显示工具提示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1732140/