1
votes

How do I use a passwordbox control with a virtual keyboard using WPF? With the textbox control, it's fairly simple to just move the caret to the next text position; not so with passwordbox, which doesn't expose the caret position.

Should I just derive my own? Seems like weak sauce.

1

1 Answers

1
votes

You can try something like this to set the selection in the PasswordBox:

private void SetSelection(PasswordBox passwordBox, int start, int length) { 
    passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic)
                         .Invoke(passwordBox, new object[] { start, length }); 
} 

After that, call it like this to set the cursor position:

// set the cursor position to 2... or length of the password 
SetSelection( passwordBox1, 2, 0); 

// focus the control to update the selection 
passwordBox1.Focus(); 

The above answer is provided by Andrew Jackson and it works fine.