MessageBox stuck in loop
-
I have a search dialog form with a text box where the user enters the search terms and a button which executes the search. The text box traps the keyup event so that the user can just press enter and initiate the search instead of clicking on the button. When the search does not return any results, a message box pops up informing the user. The problem is that if the user clears the message box by pressing enter instead of clicking the OK button, the text box gets the keyup event again, performs the search again, puts up the message box... I was able to avoid this by disabling the textbox before showing the message box and then reenabling it after but this moves the focus to the next control. Moving the focus back to the text box (in code) causes the same behavior. Does anyone know of another way to fix this? Joe
-
I have a search dialog form with a text box where the user enters the search terms and a button which executes the search. The text box traps the keyup event so that the user can just press enter and initiate the search instead of clicking on the button. When the search does not return any results, a message box pops up informing the user. The problem is that if the user clears the message box by pressing enter instead of clicking the OK button, the text box gets the keyup event again, performs the search again, puts up the message box... I was able to avoid this by disabling the textbox before showing the message box and then reenabling it after but this moves the focus to the next control. Moving the focus back to the text box (in code) causes the same behavior. Does anyone know of another way to fix this? Joe
Try to use the textbox's KeyPress event instead of KeyUp:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtFileName_DEV.KeyPress If e.KeyChar = Chr(13) Then If TextBox1.Text = "" then 'Just jump out of the sub so your code does not try to search for nothing exit sub End If 'Reaction when the enter key gets pressed End If End Sub
My advice is free, and you may get what you paid for.
-
Try to use the textbox's KeyPress event instead of KeyUp:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtFileName_DEV.KeyPress If e.KeyChar = Chr(13) Then If TextBox1.Text = "" then 'Just jump out of the sub so your code does not try to search for nothing exit sub End If 'Reaction when the enter key gets pressed End If End Sub
My advice is free, and you may get what you paid for.
Perfect! That did the trick. Thank you very much, Joe