[英]MessageBox doesn't take focus
When I click enter-button, MessageBox
is shown. 单击“输入”按钮时,将显示MessageBox
。 I want MessageBox
to close when I click enter-button again as usual. 当我像往常一样再次点击进入按钮时,我希望MessageBox
关闭。 Problem is - it doesn't have focus, but TextBox
has and when I click enter-button _textBox_OnKeyUp
eventhandler is invoked again and again. 问题是 - 它没有焦点,但TextBox
有,当我点击enter-button _textBox_OnKeyUp
调用eventhandler。 How can I solve my problem? 我怎样才能解决我的问题?
Markup: 标记:
<Grid>
<TextBox Name="_textBox"
Width="100"
Height="30"
Background="OrangeRed"
KeyUp="_textBox_OnKeyUp"/>
</Grid>
Code: 码:
private void _textBox_OnKeyUp(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
return;
MessageBox.Show("Bla-bla");
}
You could use KeyDown
event instead because the MessageBox
responds to the KeyDown
event: 您可以使用KeyDown
事件,因为MessageBox
响应KeyDown
事件:
<TextBox Name="_textBox"
Width="100"
Height="30"
Background="OrangeRed"
KeyDown="_textBox_OnKeyDown"/>
And: 和:
private void _textBox_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
return;
MessageBox.Show("Bla-bla");
}
I recomend using this method of Messagebox. 我建议使用Messagebox的这种方法 。
MessageBox.Show(Window, String)
Taken from the MSDN : 摘自MSDN:
Displays a message box in front of the specified window. 在指定窗口前显示一个消息框。 The message box displays a message and returns a result. 消息框显示一条消息并返回结果。
You can use this as the following : 您可以使用以下内容:
MessageBox.Show(Application.Current.MainWindow, "I'm on top of teh window so I should get focus");
EDIT : 编辑:
You should give back the focus to your main window before calling the MessageBox. 在调用MessageBox之前,您应该将焦点返回到主窗口。
private void _textBox_OnKeyUp(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter)
return;
//this.Focus() or at least YourWindow.Focus()
MessageBox.Show("Bla-bla");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.