In a TextBox::KeyDown event I check to see if the Enter Key has been pressed, and if so, I perform some action then attempt to select all of the text using SelectAll(), as follows:
Expand|Select|Wrap|Line Numbers
- private void SelectAllTextBox_KeyDown(object sender, KeyEventArgs e)
- {
- if (Keys.Enter == e.KeyCode)
- {
- PerformSomeAction();
- SelectAllTextBox.SelectAll();
- }
- }
To get passed the issue I changed to code to use a Timer as follows:
Expand|Select|Wrap|Line Numbers
- private void SelectAllTextBox_KeyDown(object sender, KeyEventArgs e)
- {
- if (Keys.Enter == e.KeyCode)
- {
- PerformSomeAction();
- HackTimer.Start();
- }
- }
- private void HackTimer_Tick(object sender, EventArgs e)
- {
- HackTimer.Stop();
- SelectAllTextBox.SelectAll();
- }
Note: the PerformSomeAction() method is irrelevant.
Can someone explain the timing involved and why, without the Timer, it doesn't work?