DJ.*_*DJ. 25 c# tooltip winforms
当鼠标悬停在禁用的控件上时,我正在尝试显示工具提示.由于禁用的控件不处理任何事件,我必须在父表单中执行此操作.我选择通过处理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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码确实处理禁用控件的工具提示显示.问题是当鼠标悬停在禁用的控件上时,工具提示会一直关闭并重新显示.从我在工具提示中添加的显示时间开始,当鼠标位于父窗体上方时,MouseMove事件大约每3秒调用一次,因此工具提示每3秒更新一次.但是当鼠标在禁用的控件上时,工具提示每1秒刷新一次.此外,当工具提示在表单上方刷新时,只有文本会通过简短的闪存进行更新.但是当工具提示在禁用的控件上方刷新时,工具提示窗口会关闭,就像鼠标移动到启用的控件中一样,工具提示应该关闭.但随后工具提示立即重新出现.
有人能告诉我为什么会这样吗?谢谢.
ser*_*nko 14
只有当鼠标击中提取的控件时才能显示工具提示,然后在鼠标离开时隐藏它.请看下面的代码,它应该显示表单上所有禁用控件的工具提示消息
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;
}
}
Run Code Online (Sandbox Code Playgroud)
希望这有帮助,问候
答案结果有点简单,但需要随时应用.
void OrderSummaryDetails_MouseMove(object sender, MouseEventArgs e)
{
Control control = GetChildAtPoint(e.Location);
if (control != null)
{
string toolTipString = mFormTips.GetToolTip(control);
this.mFormTips.ShowAlways = true;
// trigger the tooltip with no delay and some basic positioning just to give you an idea
mFormTips.Show(toolTipString, control, control.Width / 2, control.Height / 2);
}
}
Run Code Online (Sandbox Code Playgroud)