Solved

Need assistance with a key press event.

Posted on 2014-04-28
5
372 Views
Last Modified: 2014-04-29
Experts, I have a listview full of pricing for the user. To make it easy to find items I have a textbox for them to enter a string to search for. I have a "Search" button that will use the users search string as criteria for a query. Then I repopulate the listview with the results. everything is working just fine. My problem is that I put some code in the textbox keypress event so the user can just press enter rather than clicking the "Search" button. I check the key that was pressed for chr(13) and it is a match then i call the "Search" button (btnSearch) click event. What is happening is that when you press the "Enter" key it does what it should, but I get a loud BEEP. I cannot figure how to prevent this.

Please tell me how to correct this or is there a better approach.
    Private Sub txtSearch_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSearch.KeyPress
        If e.KeyChar = Chr(13) Then btnSearch.PerformClick()
    End Sub

Open in new window

0
Comment
Question by:Basicfarmer
  • 2
  • 2
5 Comments
 
LVL 40
ID: 40028611
See what is in btnSearch.PerformClick.

Or treat Enter in the way that Microsoft built into the forms.

In the Form properties, you have an AcceptButton property. Set it to your button, and it's Click event will automatically be called when the user press Enter from anywhere on that form.
0
 

Author Comment

by:Basicfarmer
ID: 40028615
Well, that is something i dont quite understand. There really is no performClick event for the button. But that is the only thing intellisense will give me. I get errors trying to use btnSearch.Click or btnSearch_Click.

Your solution using the forms method would work however i have other fields in the form that i do not want the btnSearch_Click method to be called if they press enter.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 40028634
My mind was somewhere else. There is not PerformClick because it is a method from the Button class. Lazy answer from my side, sorry.

Calling btnSearch.Click will definitively not work, because Click is not a method in the Button.

But usually, calling btnSearch_Click should not give you an error. You simply need to pass arguments that won't give problems to the method. If you pass back the sender and e parameter that you received in the KeyPress, it won't work because Click and KeyPress do not have the same type on the e parameter.

If btnSearch_Click does not make use of the sender or e parameters in its code, you can simply call the Click with btnSearch_Click(Nothing,Nothing) and won't have any problem.

Otherwise, please tell us what errors you get when you try to call it, and we might be able to solve it for you.

Another solution would be to set the AcceptButton property to your button only in the Enter event of the TextBox, and reset it back to Nothing in the Leave event. This way, the mechanism would work only when the user is in the TextBox. Or Enable and Disable, or change the Visible property of the button in the same events, so the button won't be available unless the cursor is in the TextBox.
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 40029273
Just set the Handled property to true:

      Private Sub txtSearch_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSearch.KeyPress
            If e.KeyChar = Chr(13) Then
                  e.Handled = True
                  btnSearch.PerformClick()
            End If
      End Sub
0
 

Author Closing Comment

by:Basicfarmer
ID: 40029609
Thanks guys both of you gave me a solution that would work. Eric's solution made more sense to me.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now