I'm trying to highlight text in every controls as it enters. It works fine when I'm using MouseClick event. But when using Enter event, it doesn't work. I need to use Enter event instead of MouseClick event because I want to highlight the text no matter how each control gets focus. I can debug the Enter event, and can see that it's processing through. But for some reason SelectAll() is not being processed at all. Why the different behavior?
// This works
private void ClientEdit_MouseClick(object sender, MouseEventArgs e)
{
Control c = sender as Control;
if (c is TextBox) (sender as TextBox).SelectAll();
else if (c is MaskedTextBox) (sender as MaskedTextBox).SelectAll();
}
// This does not work
private void ClientEdit_Enter(object sender, EventArgs e)
{
Control c = sender as Control;
if (c is TextBox) (sender as TextBox).SelectAll();
else if (c is MaskedTextBox) (sender as MaskedTextBox).SelectAll();
}
I've found a post about 2 years ago by @Jack-Fairfield, shown below, and it works!
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}
private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
txtbox.SelectAll();
}
I just started here and don't have enough reputation to give him the credit. Thanks.