Winforms 当焦点位于特定文本框中时,如何显示工具提示?

Winforms 当焦点位于特定文本框中时,如何显示工具提示?,winforms,.net-2.0,tooltip,Winforms,.net 2.0,Tooltip,对于文本框,我希望在焦点位于文本框上时立即显示工具提示,并在焦点持续时间内保持该提示,而不仅仅是当鼠标悬停在文本框上时 如何执行此操作?使用System.Windows.Forms.ToolTip并在文本框GotFocus事件中显示,然后将其隐藏在LostFocus事件中: void textBox_GotFocus(object sender, EventArgs e) { toolTip.Show("your tip", textBox); } void textBox_Lost

对于文本框,我希望在焦点位于文本框上时立即显示工具提示,并在焦点持续时间内保持该提示,而不仅仅是当鼠标悬停在文本框上时


如何执行此操作?

使用System.Windows.Forms.ToolTip并在文本框GotFocus事件中显示,然后将其隐藏在LostFocus事件中:

void textBox_GotFocus(object sender, EventArgs e)
{
    toolTip.Show("your tip", textBox);
}

void textBox_LostFocus(object sender, EventArgs e)
{
    toolTip.Hide(textBox);
}

Enter
Leave
事件在这里可能很有用,并以0的持续时间显示它以保持在那里

private ToolTip tt;

private void textBox1_Enter(object sender, EventArgs e) {
  tt = new ToolTip();
  tt.InitialDelay = 0;
  tt.IsBalloon = true;
  tt.Show(string.Empty, textBox1);
  tt.Show("I need help", textBox1, 0);
}

private void textBox1_Leave(object sender, EventArgs e) {
  tt.Dispose();
}

注意:像在我的示例中一样,两次调用
Show(…)
方法将强制“指针”正确指向控件。

已测试,事件名称:

   private void textbox_Enter(object sender, EventArgs e)
    {
        toolTip1.Show("your tip here", textbox);

    }

    private void textbox_Leave(object sender, EventArgs e)
    {
        toolTip1.Hide(textbox);

    } 

工具提示是一个控件,需要从工具箱中添加。

使用
鼠标悬停
鼠标离开
事件

    private void textBox1_MouseHover(object sender, EventArgs e)
    {
        toolTip1.Show("your tip here", textBox2);

    }

    private void textBox1_MouseLeave(object sender, EventArgs e)
    {
        toolTip1.Hide(textBox2);
    }
>

Windows窗体


你知道为什么这些事件在
Enter
Leave
do时不会出现在Visual Studio属性窗格中吗?public Constructor(){txtrl.ForeColor=Color.Gray;txtrl.GotFocus+=txtrl\u GotFocus;txtrl.LostFocus+=txtrl\u LostFocus;}private void txtrl\u GotFocus(object sender,EventArgs e){txtrl.Text=“;txtrl.ForeColor=Color.Black;}私有void txtrl_LostFocus(object sender,EventArgs e){if(string.IsNullOrWhiteSpace(txtrl.Text))txtrl.ForeColor=Color.Gray;}我建议在休假事件中检查tt是否为null。在某些情况下,可能不会触发Enter事件,然后在休假事件中会出现null引用错误。
public partial class FormWindow : Form
{
        //Constructor
        public FormWindow()
        {
            txtUrl.Text = "Enter text here";
            txtUrl.ForeColor = Color.Gray;
            txtUrl.GotFocus += TxtUrl_GotFocus;
            txtUrl.LostFocus += TxtUrl_LostFocus;
        }

        private void TxtUrl_GotFocus(object sender, EventArgs e)
        {
            txtUrl.Text = "";
            txtUrl.ForeColor = Color.Black;
        }

        private void TxtUrl_LostFocus(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtUrl.Text))
            {
                txtUrl.Text = "Enter text here";
                txtUrl.ForeColor = Color.Gray;
            }
        }
}