Selecting text in a TextBox passed to a User Control
Posted on 2004-11-17
I have designed a simple User control to allow users to select a date from a MonthCalendar and have it displayed in a TextField.The User Control is a button which takes a TextBox from a Form as a property passes it to a form with a MonthCalendar object and then sets the text property to the date the user selected.
First, the reason I have not used the DatePicker is because the dates are coming from data bound textboxes on a Form. Some dates may be null and the DatePicker does not do to well with null dates. (The checkbox property displays grayed out dates that mean nothing to the user).
Everything works as I intially expected.
The problem I am having is with the DataBindings on the text field. When the date is changed I need the underlying bound DataTable to fire the ColumnChanging Event so I can evaluate the input. However when the UserControl changes the text on the Textbox I am not able to set input focus back to the original text field. Without the input focus the ColumnChanging event is not fired. If I manually go back and select the text on the form and then tab out the ColumnChangingEvent is fired.
*Here is what I have tried so far*
1. I tried calling the Textboxes Select, SelectAll, Focus, SelectionStart, SelectionLength methods from within the UserControl to no avail.
2. I then created an event in the UserControl that fires after the Text is entered. The event is handled by the form which calls a Delgate sub on the form to Select the Text in the Textbox using Invoke. (I did this because I thought there might be a thread context issue). This doesn't work either.
I was wondering if I am just missing something simple here. If the control can modify the text of the TextBox on the form why can't it select the same text?
Any help would be greatly appreciated.