Keypress in a textbox.

  • Thread starter Thread starter Todd Sparks
  • Start date Start date
T

Todd Sparks

I am using Visual Studio 2003 to develop a windows application in which I
allow the users to just press Enter, while the focus is on a textbox, in
order to perform a search. My problem is that sometimes Keypress event
stops being raised. I have found that I can reproduce this behavior by
performing a search, opening up another form, then trying to perform another
search. The second search will NEVER work.

How can I fix this problem?

Thanks,
Todd Sparks
 
I think we will need some simplified code that demonstrates the problem to
help you out.

Chris
 
Here is the code that I am using.

Private Sub customerNumberSearch(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtCustomerNumber.KeyPress
If e.KeyChar = Chr(13) Then
'do search
End If
End Sub
 
That does not give me enough information to reproduce the error.

Chris
 
Does the second form you're bringing up have a button that is the form's
default? That might be stealing your keypress event so it's never raised.

Brian
 
When you say opening up another form, do you mean another instance of
the same form?

Aaron
 
Hi

I agree with Brian's suggestion.
Also I can not reproduce the problem with the code below.
Private Sub customerNumberSearch(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtCustomerNumber.KeyPress
If e.KeyChar = Chr(13) Then
dim fm as new form
fm.show
End If
End Sub

You may have a try and it is better to provide a reproduce sample.
Thanks.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Todd,


When you test this, than it is maybe easier when you want to emulate
situoations which takes a time to try just routines as

Private Sub customerNumberSearch(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtCustomerNumber.KeyPress
If e.KeyChar = Chr(13) Then
threading.thread.sleep(7000)
End If
End Sub


That gives you in my opinion more the change to isolate your problem.

Just as idea

Cor
 
No, just opening up another form in my project.

At this point, I have been trying to use a process of elimination to
determine what is causing the problem. I have been removing steps from the
process and trying to determine which step causes the functionality to
break. Right now it seems to have something to do with the fact that when I
return to my 'main' form and change my search criteria and press the Enter
key it is trying to do the button-click that opened up the second form
instead of my search. Any ideas on how I can prevent this action?
 
No, none of my forms have default buttons. But I do think it has something
to do with this. Somehow I think my original form is remembering that the
last thing I did was press a button so whenever I am pressing the Enter key
it is trying to duplicate that action. So, I guess the real question then
is how can I prevent this from happening?
 
Hi

You may take a look at the link below.

Trapping Enter key in Windows Forms TextBox
http://www.heikniemi.net/hc/archives/000122.html

We can inhertis a Textbox and then handle its KeyDown event
public class MyTextbox : System.Windows.Forms.TextBox
{
protected override bool IsInputKey(Keys key)
{
if (key == Keys.Enter)
return true;
return base.IsInputKey(key);
}
}

private void txtTest_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
this.lblValue.Text=this.txtTest.Text.Trim();
}
}


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Right now it seems to have something to do with the fact that when I
return to my 'main' form and change my search criteria and press the Enter
key it is trying to do the button-click that opened up the second form
instead of my search.

I'm not sure I'm following you, but if it's somehow "remembering" a keypress
later, what if you set the Handled property in the event to True?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top