Selecting text in a TextBox passed to a User Control


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*
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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sample with a datepicker....

If Not dataset.Tables(0).Rows(0).Item("MYDate") Is DBNull Then
       datepicker.DataBindings.Add(New Binding("MYDate", dataset, 0))
       datepicker.Value = Now()
End If


Check link...
gdexterAuthor Commented:
Thanks for the response.

However the dates are tracking dates so a default of Now() is not an option.
Another problem is that the client wants to be able to delete dates if needed.
You are right:
There is no easy way to handle null values with DateTimePicker control shipped
with .net.
However, I have heard that the customized control by Lutz Roeder supports null
values. It is a free download that you might want to download and test out in
your project.

Here is the link to his site:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gdexterAuthor Commented:
Well I came up with a work around using the delagate sub and calling:

Form.ActiveControl = TextBox
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.