NSTextField positioning the cursor

Hi everyone,

This is a quick one, so I was in the need of change the position of the cursor of one NSTextField, because it pre-fill the field with a partial value which came from another field (Let’s say I selected a country on one field and the state field had to came with the prefix of the country code). Easy task, right? it wasn’t so obvious for me.

One way of doing this is by using the SelectedRange property of CurrentEditor property that NSTextField have.

  • NSTextField don’t have an SelectedRange property directly, but NSText did have it.
  • CurrentEditor is and object of type NSText
//We must assure that this is the selected field
currentTextField.CurrentEditor.SelectedRange = new NSRange(currentTextField.StringValue.Length, 0);

With the code of above we send the cursor to the end of the string that the field have.

Quick and easy.