Selecting text in a TextBox passed to a User Control

Posted on 2004-11-17
Last Modified: 2009-12-16

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.

Question by:gdexter
    LVL 27

    Assisted Solution

    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...
    LVL 4

    Author Comment

    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.
    LVL 28

    Accepted Solution

    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:
    LVL 4

    Author Comment

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

    Form.ActiveControl = TextBox

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now