【发布时间】:2014-09-03 19:14:34
【问题描述】:
仅当焦点在特定文本框上时,是否可以在 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(/*...*/) { /*...*/ }事件。
现在我正在这样做:
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。
有没有更好的方法?
【问题讨论】:
-
尝试在另一个控件中使用 button.visible=false 输入事件而不是在按钮控件中。所以当另一个控件获得焦点时,此按钮可见更改
-
实际上还有很多其他控件,用户可以专注于其中的任何一个。让每个领域都发生事件并不是很好。
-
使用 textBox1.GotFocus 和 textBox1.LostFocus
-
@MehdiKhademloo 它的工作原理完全相同。
-
如果你总是需要点击按钮事件,那么在按钮点击后隐藏按钮。