仅在焦点位于文本框上时显示按钮

时间:2014-09-03 06:32:04

标签: c# winforms

只有当焦点在特定文本框上时,才可以在Windows窗体上显示按钮吗?

用这种方法试过:

    private void button3_Click(object sender, EventArgs e)
    {
        MessageBox.Show("OK");
    }

    private void textBox2_Enter(object sender, EventArgs e)
    {
        button3.Visible = true;
    }

    private void textBox2_Leave(object sender, EventArgs e)
    {
        button3.Visible = false;
    }

没有运气,因为按钮点击不起作用,因为在文本框失去焦点后按钮会立即隐藏 ,从而阻止其触发button3_Click(/*...*/) { /*...*/ }事件。< / p>

现在我这样做:

    private void button3_Click(object sender, EventArgs e)
    {
        MessageBox.Show("OK");
    }

    private void textBox2_Enter(object sender, EventArgs e)
    {
        button3.Visible = true;
    }

    private void textBox2_Leave(object sender, EventArgs e)
    {
        //button3.Visible = false;
        DoAfter(() => button3.Visible = false);
    }

    private async void DoAfter(Action action, int seconds = 1)
    {
        await Task.Delay(seconds*1000);
        action();
    }

表单现在等待一秒钟,然后才隐藏button3

有没有更好的方法?

4 个答案:

答案 0 :(得分:2)

我认为只有在焦点位于特定文本框或焦点位于按钮上时才显示按钮。

要执行此操作,您可以检查Focused button3事件中Leave的{​​{1}}属性,并且只有在按钮没有焦点时才隐藏按钮。请注意,在textBox2 LeavetextBox2事件发生之前,该按钮将获得焦点

然后,您需要隐藏button3失去焦点并且焦点移动到textBox2以外某处的场景中的按钮。您可以通过处理Leave button3事件来使用完全相同的技术,只有在button3没有焦点时才隐藏textBox2

以下代码应符合您的要求:

private void textBox2_Leave(object sender, EventArgs e)
{
    if (!button3.Focused)
    {
        button3.Visible = false;
    }
}

private void button3_Leave(object sender, EventArgs e)
{
    if (!textBox2.Focused)
    {
        button3.Visible = false;
    }
}

private void textBox2_Enter(object sender, EventArgs e)
{
    button3.Visible = true;
}

private void button3_Click(object sender, EventArgs e)
{
    MessageBox.Show("Button clicked");
}

答案 1 :(得分:1)

为什么不使用TextBox的GotFocusLostFocus事件?

private void textBox2_GotFocus(object sender, EventArgs e)
{
    button3.Visible = true;
}

然后隐藏点击事件上的按钮。

private void button3_Click(object sender, EventArgs e)
{
    MessageBox.Show("OK");
    button3.Visible = false;
}

答案 2 :(得分:0)

如何添加一个面板并将按钮和文本框放在该面板中,当用户MouseHover该面板显示该按钮时...

这样用户就可以点击按钮......

这是你要找的活动,我想...... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx

<强>更新

var textboxFocussed = false;        
private void textBox2_Enter(object sender, EventArgs e)
{
    textboxFocussed = true;
}

private void textBox2_Leave(object sender, EventArgs e)
{
    textboxFocussed = false;
}

更新2

private void Panel_GotFocus(object sender, EventArgs e)
{
    button3.Visible = textboxFocussed;
}

private void Panel_LostFocus(object sender, EventArgs e)
{
    button3.Visible = false;
}

以下是Panel Events

的详细信息

答案 3 :(得分:0)

您可以在加载的表单上为所有控件添加 Enter 事件处理程序。只需确保跳过要显示按钮的控件。

    List<string> strControlException = new List<string>();

    public Form1()
    {
        InitializeComponent();
        strControlException.Add("btnMain");
        strControlException.Add("txtMain");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < this.Controls.Count;i++ )
        {
            if (!strControlException.Contains(Controls[i].Name))
            {
                Controls[i].Enter += new EventHandler(hideButton);
            }
        }
    }

    private void txtMain_Enter(object sender, EventArgs e)
    {
        btnMain.Visible = true;
    }

    private void hideButton(object sender, EventArgs e)
    {
        btnMain.Visible = false;
    }

btnMain(你想要操纵的按钮)和txtMain(控制按钮的可用性)是这里争用的控件

在表单上添加更多控件以进行测试。

上述代码的说明:

  1. 首先使用应显示Button
  2. 的控件名称初始化列表
  3. 在表单加载上为所有控件添加一个事件处理程序(列表中的控件除外)
  4. 在处理程序功能中隐藏按钮。 (您可能希望在此处基于调用此函数的控件执行更多逻辑)
  5. 按钮默认隐藏,仅在文本框中输入事件我们显示按钮。