当且仅当文本框具有焦点时,是否可以在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
。
是否有更好的方法?