Solved

Need assistance with a key press event.

Posted on 2014-04-28
5
371 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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys both of you gave me a solution that would work. Eric's solution made more sense to me.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

10 Experts available now in Live!

Get 1:1 Help Now