【问题标题】:How do I display a tooltip when focus is in a specific textbox?当焦点位于特定文本框中时,如何显示工具提示?
【发布时间】:2011-12-14 20:00:42
【问题描述】:

对于文本框,我想在焦点位于文本框上时立即显示工具提示,并在焦点期间停留在那里 - 而不仅仅是当鼠标悬停在文本框上时。

我该怎么做?

【问题讨论】:

    标签: winforms .net-2.0 tooltip


    【解决方案1】:

    EnterLeave 事件在这里可能很有用,并以 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(...) 方法两次将强制“指针”正确指向控件。

    【讨论】:

    • 我建议在 Leave 事件中检查 tt 是否为空。可能存在未触发 Enter 事件的情况,然后您将在 Leave 事件上收到空引用错误。
    【解决方案2】:

    已测试,事件名称:

       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);
    
        } 
    

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

    【讨论】:

      【解决方案3】:

      使用mouse hovermouse leave 事件

          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);
          }
      

      >

      【讨论】:

        【解决方案4】:

        Windows 窗体

        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;
                    }
                }
        }
        

        【讨论】:

          【解决方案5】:

          使用 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);
          }
          

          【讨论】:

          • 知道EnterLeave 时为什么这些事件不会出现在Visual Studio 属性窗格中吗?
          • public Constructor() { txtUrl.ForeColor = Color.Gray; txtUrl.GotFocus += TxtUrl_GotFocus; txtUrl.LostFocus += TxtUrl_LostFocus; } private void TxtUrl_GotFocus(object sender, EventArgs e) { txtUrl.Text = ""; txtUrl.ForeColor = 颜色.黑色; } private void TxtUrl_LostFocus(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtUrl.Text)) txtUrl.ForeColor = Color.Gray; }
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-07-17
          • 2022-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-13
          • 2023-04-04
          相关资源
          最近更新 更多