Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need assistance with a key press event.

Posted on 2014-04-28
5
Medium Priority
?
379 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
[X]
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
  • 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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

715 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