Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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??
Posted

Hi,

 

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

 

 

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

Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

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??

Posted

maybe this can get you on the right track.

 

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

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
Posted (edited)

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

Edited by techmanbd
Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

e.Handled wasn't the answer, the event still fires. Here's the 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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...