Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15729

Program responds multiple times to a key

$
0
0
I have a search form where the user can enter a name in an InkEdit textbox and click on the "Search" commandbutton to search.

To make it easier for the user, I need to make it possible to type the name and then just press the Enter key to invoke that commandbutton.
However, setting the Default property of the commandbutton to True, does not achieve this because the textbox is an InkEdit textbox.
Had it been a standard VB6 Textbox, pressing Enter on it would have invoked the commandbutton.
But for a bizarre reason the InkEdit box does not do that.

So, I resort to this:
Code:

Private Sub txtSearchName_KeyUp(pKey As Long, ByVal ShiftKey As Integer)
  If (pKey = 13) And (ShiftKey = 0) Then
      Call cmdSearch_Click
  End If
End Sub

Now, let's say the user presses Enter key without typing a name.
Obviously my search procedure must guard against such a situation and notify the user to specify a name to search:
Code:

Private Sub cmdSearch_Click()

  ......

  If Trim(txtSearchName.Text) = "" Then
      MsgBox "Please Enter a name.", vbOKOnly
      txtSearchName.SetFocus
      Exit Sub
  End If

  ......

End Sub

Now, let's say the user presses Enter without having typed a name.
What happens is that that Messagebox pops up.
The user presses Enter to respond to the "Ok" button of the Messagebox, and then the above code sets the focus to txtSearchName textbox.
This will unwantedly trigger the KeyUp event of the txtSearchName text box one more time, because the Enter key that the user had typed to respond to the Messagebox is STILL active (not cleared by the MessageBox) and is transferred to the InkEdit box!!!

So, that Enter key will unwantedly trigger another call to the cmdSearch_Click !!!
And that will AGAIN trigger the validation for the blank name, and pop up another Messagebox !!!
The user presses Enter to close this new Messagebox, and the whole thing repeats !!!

The Messagebox pops up again and again and again for ever !!!

The only way to break this vicious cycle is for the user to close the Messagebox by another key instead of Enter (for example by Spacebar or Escape) or by a mouse click !!!

Now, I face ANOTHER surprise:

In order to solve the above problem, I add a dummy text box to the screen and do this below code hoping to suppress that Enter key:
Code:

Private Sub cmdSearch_Click()

  ......

  If Trim(txtSearchName.Text) = "" Then
      MsgBox "Please Enter a name.", vbOKOnly
      txtDummy.SetFocus
      DoEvents
      txtSearchName.SetFocus
      Exit Sub
  End If

  ......

End Sub

Even THAT doesn't work !!!
I thought that would clear the keyboard's past keys, but that doesn't.

I really don't know what to do.
Please help.
Thanks.

Viewing all articles
Browse latest Browse all 15729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>