I am doing a WPF application with a virtual keyboard .As mentionned in the picture, there is two textbox pseudo
and password
and i'd like to enter their values using the virtual keyboard.
The problem is how to know that the cursor is in the first field or in the second one or out. I tried isfocused
but it didn't give a result.
So how can i do this task?
public partial class Authentification : Window
{
public TextBox numero = new TextBox();
bool isPseudoFocused = false;
bool isPasswordFocused = false;
public Authentification()
{
InitializeComponent();
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
if (Keyboard.FocusedElement == pseudo)
MessageBox.Show("hhhh");
}
private void un_Click(object sender, RoutedEvent e)
{
if (isPseudoFocused) pseudo.Text += "1";
if (isPasswordFocused) password.Text += "1";
}
private void pseudo_FocusableChanged(Object sender, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show("pseudo");
isPseudoFocused = true;
isPasswordFocused = false;
}
private void password_FocusableChanged(object sender, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show("password");
isPseudoFocused = false;
isPasswordFocused = true;
}
}
you should have something like this:
bool isPseudoFocused = false;
bool isPasswordFocused = false;
//when Pseudo gets focus the set
isPseudoFocused = true;
isPasswordFocused = false;
//when Password gets focus then set
isPseudoFocused = false;
isPasswordFocused = true;
//when you are typing text then you know where to put your text.
UPDATE:
you should put this code into a TextBox_GotFocus
handler.
private void pseudo_GotFocus(object sender, RoutedEventArgs e)
{
MessageBox.Show("pseudo"); isPseudoFocused = true; isPasswordFocused = false;
}
private void password_GotFocus(object sender, RoutedEventArgs e)
{
MessageBox.Show("password"); isPseudoFocused = false; isPasswordFocused = true;
}
您可以使用Keyboard.FocusedElement
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.