C#在禁用的文本框(窗体)上显示工具提示
问题描述:
我试图获得一个工具提示,以将鼠标悬停在禁用的文本框中显示。我知道,因为禁用了控件,所以以下操作无效:
I am trying to get a tooltip to display on a disabled textbox during a mouse over. I know because the control is disabled the following won't work:
private void textBox5_MouseHover(object sender, EventArgs e)
{
// My tooltip display code here
}
如何我可以在禁用控件的鼠标悬停上显示工具提示吗?
How can I get the tooltip to display on a mouse over of a disabled control?
非常感谢
答
如果禁用控件,MouseHover将不会触发。相反,您可以在Form MouseMove事件中检查是否将鼠标悬停在文本框上
MouseHover wont fire if control is disabled. Instead you can check in Form MouseMove event whether you hover the textbox
public Form1()
{
InitializeComponent();
textBox1.Enabled = false;
toolTip.InitialDelay = 0;
}
private ToolTip toolTip = new ToolTip();
private bool isShown = false;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(textBox1 == this.GetChildAtPoint(e.Location))
{
if(!isShown)
{
toolTip.Show("MyToolTip", this, e.Location);
isShown = true;
}
}
else
{
toolTip.Hide(textBox1);
isShown = false;
}
}