8

I want to select all the contents of a MaskedTextBox when the clicks (or tabs onto) the control, so they can easily replace the old content. I tried calling SelectAll() in the Enter event, but that didn't work at all.

I switched to using the GotFocus event, which works great when tabbing through controls, but doesn't work when I click on it with the mouse. I would only want to select all the contents when first entering/focusing on the control (subsequent clicks might be used to position the cursor to edit the existing text).

I added a button and tried calling SelectAll() in the button click event, but that didn't do anything either. What's going on? Is this a bug?

How can I get around this?


Steps to reproduce

  1. Create a new Windows Form Application in .NET 4.0 in Visual Studio 2010.
  2. Add a TextBox, MaskedTextBox, and Button to the default form
  3. Change the Mask property on the MaskedTextBox to "_____".
  4. Add some event handlers:

    private void maskedTextBox1_GotFocus(object sender, EventArgs e)
    {
      Debug.WriteLine("GotFocus");
      maskedTextBox1.SelectAll();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
      Debug.WriteLine("Click");
      maskedTextBox1.SelectAll();
    }
    
  5. Run the program, entered some data into the MaskedTextBox, tab through controls back to it. It selects the contents of the MaskedTextBox.

  6. Select the other TextBox. Try clicking on MaskedTextBox. Output shows that GotFocus event was called, but text doesn't get selected.
  7. Try clicking on button in form. Text doesn't get selected.

Tested in Visual Studio 2010 with .NET 4.0 in a Windows Forms Application project


Why this isn't a duplicate of TextBox.SelectAll() does not work with TAB

If you notice, the title says "SelectAll doesn't work with TAB". In my case, it does work with Tab, it doesn't work with the mouse - completely opposite scenario. The answer for that question is to use the GotFocus event. I'm already using the GotFocus event, but it doesn't work. That answer does not answer this question. It is clearly not a duplicate. If I'm wrong, please explain in the comments.

Jeff B
  • 8,572
  • 17
  • 61
  • 140
  • 1
    look [here](http://stackoverflow.com/questions/97459/automatically-select-all-text-on-focus-in-winforms-textbox), it might help – Mord Zuber Feb 18 '15 at 23:16

2 Answers2

13

Your SelectAll() is being overwritten by the default functionality of the masked textbox select. I would use the Enter event, it allows for tabbed entry or mouse click entry to the masked text box. You will most likely need to use the BeginInvoke method. Try the code below. It worked for me when I tried...

private void maskedTextBox1_Enter(object sender, EventArgs e)
{
    BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}

private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
    txtbox.SelectAll();
}
Jeff B
  • 8,572
  • 17
  • 61
  • 140
Jack Fairfield
  • 1,876
  • 22
  • 25
  • 1
    Does the `BeginInvoke` just force the `SelectAll()` to run after the default functionality of the masked textbox is executed? I'd love to hear more on why this works if you don't mind expanding your answer a bit more. – Jeff B Feb 19 '15 at 14:46
  • 1
    I found a [less wordy way](http://stackoverflow.com/a/1007612/945456) of doing BeginInvoke and have taken the liberty to edit the answer. – Jeff B Feb 19 '15 at 15:17
  • 1
    Here is some decent reading on BeginInvoke. CodeProject can explain it much better than I can. But yeah basically in your example BeginInvoke runs after the default functionality. http://www.codeproject.com/Articles/10311/What-s-up-with-BeginInvoke – Jack Fairfield Feb 22 '15 at 04:30
1

Executing Focus before Select All worked for me:

private void Masked_Enter(object sender, EventArgs e) {
    ((MaskedTextBox)sender).Focus();
    ((MaskedTextBox)sender).SelectAll();
}
  • Does this work when the masked text box gets focus by being clicked on? It worked in other scenarios for me, but not that one. – Jeff B Oct 05 '20 at 16:11