MarkJuster

Hi,

I need to allow users to go to the next control by pressing Enter rather than tab. Easily done, you just intercept the enter key on the key up and go to the next field.

However, if you put a MessageBox in the key up event and the user presses the enter key to acknowledge the message box, the enter key is sent to the next textbox which in turn makes it navigate to the next textbox, so the form skips a textbox. It's fine without a message box. I'd like to understand why this happens.

Private Sub TextBox4_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox4.KeyUp

If e.KeyCode = Keys.Enter Then
e.Handled = True
MessageBox.Show("Hi there")

ProcessTabKey(True)
End If

End Sub

I also run into a related problem where the user enters a value into a textbox and presses enter and I have to retrieve data from the database, or if no data is found, take the user to a new form. Again, I use the key up event to intercept the enter key. I display a messagebox telling the user there was no data for the value they entered. If the press Enter in the message box, when I display the new form, the enter key press flow through the new form.

Any help is appreciated. I've been looking around for information on this and I haven't found a solution.
I'm using .Net 1.1.

Thanks
Mark


Re: Windows Forms General MessageBox enter key activates form controls.

decyclone

Hi Mark,

The problem is with processing the KeyUp event here. When you press a key, three messages are created, KeyDown, KeyPress and KeyUp. The MessageBox processes the KeyDown or KeyPress events of the Enter key. So, KeyUp message is not processed by it and it is posted to the Window/Control activated after the MessageBox is closed.

The solution is to process KeyUp or KeyPress events in the TextBoxes in your forms.

Hope this helps.





Re: Windows Forms General MessageBox enter key activates form controls.

MarkJuster

Thanks,

I'm not sure how it helps. I seem to run into this problem because the next textbox has to process the enter key as a tab as well.
So I have
TextboxA, TextBox B, TextBox C

Pressing enter in any of the textboxes needs to move the focus to the next textbox. It works fine without the messagebox. Each textbox has the KeyUp event handler to process the enter key.

If the messagebox is shown in the key up event of TextBoxA, the keyup event of Textbox B catches the enter key from the messagebox immediately after the messagebox closes. I can't tell in the code if the enter key is from the messagebox or from the user pressed the enter key.

So although I could process the enter key in the next textbox after the Messagebox, I would then lose the ability to move to the next textbox

I have tried combinations of key up, key press and key down event handlers, but nothing makes a difference (except not processing the enter key in Textbox B) .

Mark




Re: Windows Forms General MessageBox enter key activates form controls.

decyclone

Can you process KeyDown event in the TextBoxes instead of KeyUp event to make it act as a Tab I think that will solve the problem...





Re: Windows Forms General MessageBox enter key activates form controls.

sunil_kor

Have just tried your problem. The KeyUp event is indeed curious.

However, it works fine for KeyPress event.

I have tried combinations of key up, key press and key down event handlers, but nothing makes a difference (except not processing the enter key in Textbox B) .

Pls. make sure that you're disabling all other key event logic for the textboxes except KeyPress.





Re: Windows Forms General MessageBox enter key activates form controls.

nobugz

Paste this code into your form:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Enter) {
this.SelectNextControl(this.ActiveControl, true, true, true, true);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}






Re: Windows Forms General MessageBox enter key activates form controls.

MarkJuster

Everyone's probably moved on but this solution with sunil_kor's KeyDown suggestion both work.

Sorry for the delay responding, I was out of town demonstrating at a client site so it took a while to get back and try out these suggestions.

Thanks
Mark