bool IsShown = false;
void Form1_MouseMove(object sender, MouseEventArgs e)
{
Control ctrl = this.GetChildAtPoint(e.Location);
if (ctrl != null)
{
if (ctrl == this.button1 && !IsShown)
{
string tipstring = this.toolTip1.GetToolTip(this.button1);
this.toolTip1.Show(tipstring, this.button1, this.button1.Width /2, this.button1.Height / 2);
IsShown = true;
}
}
else
{
this.toolTip1.Hide(this.button1);
IsShown = false;
}
}
以上的TextBox是在form上是可以用这个的。
如果textBox在GroupBox或Panel上则不能用以上的代码了。
就得把TextBox的ReadOnly=true然后在TextBox_MouseMove()中写入如下代码
private void textBox1_MouseMove(object sender, MouseEventArgs e)
{
string tipstring = this.toolTip1.GetToolTip(this.textBox1);
this.toolTip1.Show(tipstring, this.textBox1, this.textBox1.Width / 2, this.textBox1.Height / 2);
}
//离开鼠标的的事件
private void textBox1_MouseLeave(object sender, EventArgs e)
{
this.toolTip1.Hide(this.textBox1);
}