Alex Rivera | Logout

How can i set the caret position to a specific index in passwordbox in WPF

Asked 2009-06-13T08:26:56.920
11

I need to set the cursorposition inside the passwordbox explicitly in WPF. I couldn't see the selectionstart property in passwordbox.

Any help?

Edit
Report

1 Answer

26

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...
SetSelection( passwordBox1, 2, 0);

// focus the control to update the selection
passwordBox1.Focus();
answered 2009-06-26T01:09:01.883

Your Answer