How can I set the caret position to a specific index in the password field in WPF

I need to position the cursor in an input box explicitly in WPF. I could not see the selectstart property in the password.

Any help?

+4
source share
2 answers

You can try something like this to set the highlight in 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 in such a way as to set the cursor position:

 // set the cursor position to 2... SetSelection( passwordBox1, 2, 0); // focus the control to update the selection passwordBox1.Focus(); 
+10
source

No, the API for PasswordBox does not provide a way to do this.

+1
source

Source: https://habr.com/ru/post/1286167/


All Articles