Hi all! I want to always focus on a specific textbox on my WPF application whenever i click on anything on the applciation it should always focus on the textbox. Is this possible and how please advice and help. Thanks!!!
A:
There is an event handler MouseLeftMouseButton. When the event handler was triggered, use textbox.Focus() inside the handler.
apoc29
2010-02-11 08:37:47
That would only work if the focus is lost by using the mouse. But what if somebody press the Tab-key ...?
Marcel Benthin
2010-02-11 08:56:49
A:
Add to the TextBox.OnLostFocus event a handler which sets the focus to the TextBox.
Marcel Benthin
2010-02-11 08:52:06
+1
A:
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;
}
Veer
2010-02-17 07:16:36