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!