开发者

WPF always focus on a textbox

I want to always Focus on a specific TextBox on my WPF application whenever I click on anything on the ap开发者_StackOverflow社区plication it should always focus on the TextBox.


Add to the TextBox.OnLostFocus event a handler which sets the focus to the TextBox.


There is an event handler MouseLeftMouseButton. When the event handler was triggered, use textbox.Focus() inside the handler.


If i'm right, your intention is to get the keyboard commands and display the char pressed into your textbox even if the focus is on other controls.

If that is the case, you can route the keyboard commands to the root control (control in the top level... eg: window), analyse them and display in the textbox. I'ld try to give examples if that helps.

EDIT:

private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
  if (Keyboard.Modifiers != ModifierKeys.Shift)
  {
    if (e.Key > Key.A && e.Key < Key.Z)
    {
      textBox1.Text += e.Key.ToString().ToLower();
    }
  }
  else
  {
    if (e.Key > Key.A && e.Key < Key.Z)
    {
      textBox1.Text += e.Key.ToString();
    }
  }            
  e.Handled = true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜