• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

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.

  • 2
2 Solutions
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:

gdexterAuthor Commented:
Well I came up with a work around using the delagate sub and calling:

Form.ActiveControl = TextBox

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now