Reputation: 93
In my winforms application for visual studio 2010,
I have a button, and two combobox(combobox1
,combobox2
).
I have added code in button to clear the previously entered data in first combobox(combobox1) and set focus to it.
In keyup event of first combobox(combobox1
), I have checked for enter key, if pressed, focus will move to next combobox(combobox2
).
But my problem is, when i press button(by pressing enter while focus is on button), focus is directly moved to last(second) combobox(combobox2
).
Keyup event of combobox1 is automatically fired though i have entered on button only
How can i solve this?
My code is give below
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'code to perform database action
Me.ComboBox1.Focus()
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
If e.KeyCode = Keys.Enter Then
If Me.ComboBox1.SelectedText = String.Empty Then
ComboBox2.Focus()
End If
End If
End Sub
Update
When i click on button, problem do not appear But when i press enter while focus is on button, to fire the button click event, problem occurs.
Upvotes: 0
Views: 2273
Reputation: 11627
The easiest way to fix this is to change ComboBox1 to be a KeyDown handler not key up.
The reason it happens is the sequence of events:
Button2_Click
), focus moves to ComboBox1ComboFox1_KeyUp
), focus moves to ComboBox2Edit:
A more wordy explanation: You press the enter key down on button1
, which causes it's OnKeyDown handler to run. By the time you have released the enter key, focus is on Combobox1
, and so it's OnKeyUp handler runs.
Upvotes: 2