Prevent Enter on Messagebox to fire KeyUp event

jccorner

Centurion
Joined
Jan 31, 2004
I have a search field that when you hit enter it fires the keyup event and loads the grid. Well, in certain cases the field has to be numeric depending upon the search criteria. Well if it is not a messagebox appears and tells the user to enter a number, the problem is if the user hits the enter key, the messagebox is killed but then the keyup event is fired again therefore reexecuting the messagebox because the search field still has a number in it. Pretty much this is an endless loop unless the user actually hits the button using the mouse. Does anyone know how to prevent the keyup event from being fired or how I can detect that the enter key was pressed from a messagebox??
 

rfazendeiro

Centurion
Joined
Mar 8, 2004
Hi,

well you could put a if in the start of your event to check if the user pressed the enter key


Code:
private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
	if (e.KeyCode != Keys.Enter)
	{
            //Put your code here
	}
}
 

jccorner

Centurion
Joined
Jan 31, 2004
rfazendeiro,

I still want the search field to detect when the enter key is hit so that it can load the grid. What I don't want is for the keyup event to get fired when enter is hit from the messagebox.
 

Cags

Contributor
Joined
Feb 19, 2004
Location
Melton Mowbray, England
You could try setting a public boolean when the messagebox is displayed, and set it back after it has been closed. Then in the key event you check if the key is enter and the bool is the right value. I've not checked this, but the theory is sound.
 

jccorner

Centurion
Joined
Jan 31, 2004
Cags,

Your theory was not totally sound. It does solve my problem, but it also creates a new one.

When the message box appears, I set the boolean to true. If the user does press enter, the keyup event is still fired but since the boolean is true it does not enter the code but it does set the boolean to false now allowing for future key press of the enter key.

Here is the bug:

The thing is when the message box appears, the boolean is set to true. If the user does in fact use the mouse to click the ok button, the keyup event is never fired therefore not setting the boolean back to false. I now actually have to hit the enter button twice in the search field to fire the keyup event.

Does anyone know how I can detect when the message box has been closed??
 

techmanbd

Junior Contributor
Joined
Sep 10, 2003
Location
Burbank, CA
maybe this can get you on the right track.

Code:
        Dim result As DialogResult
        result = MessageBox.Show("wahtever you put here")
        If result = DialogResult.OK Then
            'code here
        End If
 

jccorner

Centurion
Joined
Jan 31, 2004
The result is always going to be okay regardless whether the enter key or button is pressed. What I need is the ability to differentiate when the enter key was hit on the messagebox from when there is a mouse click on the messagebox.
 

Cags

Contributor
Joined
Feb 19, 2004
Location
Melton Mowbray, England
I spent ages trying to emulate your problem without success before I finally realised your using the keyup event and I was using the keydown event. Would it be possible for you to use the keydown event as this seems to stop the problem.
 

techmanbd

Junior Contributor
Joined
Sep 10, 2003
Location
Burbank, CA
ok, I am assuming that you have an if statement to call the messagebox? if so in that if statement put
e.handled = true

and if this isn't what you are looking for, maybe if you put up the code you are working on, you can get a better answer quicker
 
Last edited:

jccorner

Centurion
Joined
Jan 31, 2004
e.Handled wasn't the answer, the event still fires. Here's the code:

Code:
Private Sub LoadGrid()
   If mskSFrom.Text.Trim().Length() > 0 then
   If IsNumeric(mskSFrom.Text) Then
         'Loads the grid
   Else
                        MsgBox("Please enter a number.", MsgBoxStyle.Exclamation, Globals.ProdName)
                        txtSFrom.Text = String.Empty
                        txtSFrom.Focus()
                        Exit Sub
                    End If
   End If
End Sub

Private Sub SearchField_KeyUp(ByVal sender As Object, ByVal e As System.windows.Forms.KeyEventArgs) Handles mskSTo.KeyUp, mskSFrom.KeyUp
If e.KeyCode = Keys.Enter Then
            Me.Cursor = Cursors.WaitCursor

                If mskSFrom.Text.Trim().Length() > 0 Then
                    LoadGrid()
                End If
            End If

            Me.Cursor = Cursors.Default
            e.Handled = True
End Sub

I fixed my problem by blanking out the field when it was not a numeric value but I do realize I'm going to run into this issue again later on so I'm curious as how it would be solved when a simple workaround is not available.
 
Top Bottom