Button.PerformClick Woes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

To do a search and populate a listbox when the user presses Enter, I have
implemented btnGo.PerformClick method in the SearchTextBox.KeyPress as
follows:

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)
If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
If Not mGoButton Is Nothing Then
mGoButton.PerformClick()
End If
End If
End Sub

It is working fine except for one problem - the system makes an alert sound
everytime (the kind you hear when you click outside a modal dialogbox). How
can I stop this sound? Thanks
 
Jav said:
To do a search and populate a listbox when the user presses Enter

Assign the button to the form's 'AcceptButton' property. No additional code
is required, the button's 'Click' event handler will be called when pressing
the Return key.
 
Thanks, my Form has more than one UserControl, each with a different Go
button. Should I dynamically set the Form's AcceptButton depending upon the
UserControl that is currently active?
 
I found the source of my problem. I thought I'll share it for the benefit of
the others.

The Ding Alert sound I am hearing is caused by the Enter keypress running
loose in the Form. Of course, if the AcceptButton was set up, that would
take care of it, but unless someone is developing really, really small Apps,
this technique is too cumbursome. What I needed to do in my TextBox.KeyPress
was to kill the 'Enter' after doing my thing.

Now in my keypress I call
e.handled = True
before exit and everything is working good.

Jav
 
Back
Top