in

QuestMaster.NET

A Quest for the Perfect Program, and a Quest for Life

Life, Universe And Everything According To Dirk

We apologize for the inconvenience

November 2007 - Posts

  • MaskedTextBox and SelectAll - or why MSDN Community Content is cool

    I was working on one of my pet projects the other day, and wanted to implement this behavior: I have a text box for entering a time, and I use a masked textbox for that purpose.  The text box has a default value, but the text should be selected so that users can start typing a new time right away - without having to select the existing value first.  My first thought was simple:

            private void maskedTextBox1_Enter(object sender, EventArgs e)

            {

                maskedTextBox1.SelectAll();

            }

    That didn't work as expected, however.  The text in the text box was not selected, even though maskedTextBox1.SelectedText returned the entire content of the checkbox.

    After some digging around on MSDN, I found a community content entry for TextBoxBase.SelectAll().  It links to a discussion on the Microsoft Windows Forms Forum, that describes my problem and offers a solution:

            private void maskedTextBox1_Enter(object sender, EventArgs e)

            {

                this.BeginInvoke((MethodInvoker)delegate

                {

                    maskedTextBox1.SelectAll();

                });

            }

    I think this is a very good example of helpful community content in MSDN!

    Posted Nov 22 2007, 10:42 by Dirk with no comments
    Filed under:
Copyright Dirk Rombauts
Powered by Community Server (Non-Commercial Edition), by Telligent Systems