Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Need assistance with a key press event.

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

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

790 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