Messagebox Keystrokes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When a messagebox is displayed with Yes/No buttons
and Enter is used to response, a keypress for Enter is
created. How do I cancel this keypress?

I use Enter to move to the next control.
Answering a messagebox with the "Enter" key
results in a move to the next control which
I do not want to happen.
 
Put 2 textboxes on a form
Add the following code:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = System.Windows.Forms.Keys.Return Then
e.Handled = True
System.Windows.Forms.SendKeys.Send("{tab}")
End If
End Sub

Private Sub TextBox2_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyUp
If e.KeyCode = System.Windows.Forms.Keys.Return Then
e.Handled = True
If MessageBox.Show("Continue?", "Continue",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
System.Windows.Forms.SendKeys.Send("{tab}")
End If
End If
End Sub

Private Sub TextBox3_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyUp
If e.KeyCode = System.Windows.Forms.Keys.Return Then
e.Handled = True
System.Windows.Forms.SendKeys.Send("{tab}")
End If
End Sub

Run the code
Answer Y to the message and focus moves to Textbox3
Answer using the Enter key and focus moves to Textbox1

In which keypress event are you referring to?

Thanks,
Mary
 
I figured it out.
I moved TextBox1 and 3 from Keyup to Keydown
and changed
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.Return Then
If MessageBox.Show("Continue?", "Continue",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
System.Windows.Forms.SendKeys.Send("{tab}")
Else
e.Handled = True
End If
End If
End Sub

And my new program works the way that I want it to.

Thanks for your assitance
Mary
 
The Enter key in this case will be trapped by MessageBox. I suppose
MessageBox.Show should return a default button that you didn't show in
your example:

If MessageBox.Show("Continue?", "Continue", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, [what goes here?]) = DialogResult.Yes Then
System.Windows.Forms.SendKeys.Send("{tab}")
Else
[do something]
End If



Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Back
Top