1
votes

two text box : pseudo and password and virtual keyboard

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;
    }
}
2
I think somehow you should specify which TextBox you want to write.Dilshod

2 Answers

2
votes

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;
}
1
votes

You can use Keyboard.FocusedElement for this