Saturday 2 January 2010

Setting Focus and Highlighting Text

In the last post I talked about setting focus back to a field when a validation check had failed. Usually when the focus is reset to a field that the user tried to get out of the text is highlighted, usually to indicate something is wrong with it.

Remember the validation event is oly triggered when focus is lost in a field i.e. when the user tries to move on to the next field or exit. If the validation fails the focus has to be returned to the field with the incorrect data in it.

If you want to highlight the text in the field you can use this after you set focus:

txtField.SelectionStart = 0

This piece of code puts the cursor at the start of the field.

txtField.SelectionLength = txtField.Text.Length


This piece of code sets the length of selection as the length of the text in the field, so it only selects that text and no more.

No comments:

Post a Comment