TextBox_KeyPress Event

H

Hal Gibson

Because of a legacy (originally DOS) Sub
Procedure "AlphaInput" that is called in thousands of
places in our code, I need to be able to set a
variable, "KeyedString",to the value of TextBox.Text
(KeyedString originally was set by tracking each
individual key pressed, independently of textbox.text)

Since we're setting KeyedString during the keypress
event, Textbox.Text is always one character behind.

That's OK if the user presses <Enter> or <Tab> after the
last printable character is keyed, because this causes
another trip through Alphainput after that last character
has appeared in the TextBox.

If the user causes the TextBox to lose focus without
pressing <Enter> or <Tab> - say by clicking a Command
Button - the final alphainput isn't triggered, and
KeyedString ends up missing the last printable character
entered.

I'm wondering if there is some way to capture the
anticipated TextBox.Text string during the Keypress event
before the final character displays in the textbox,
perhaps some property I've missed?
 
M

Menu Chen

My friend, I think you can Use the event called Lost_Focus
of TextBox.Fint it out and write your code in.I think it
will be Ok.
Sorry for my poor English
Best Regards.
 
E

EricJ

in addition you can also have a look at the keydown event, you can find the
key the user typed in the e parameter

hope it helps

eric
 
C

Cor

Hi Hal,

I just made an example for W.Adams two rows beneath this, it is not exactly
your problem, but there is enough code in it so solve your problem yourself
I think.

If not message than again and I take a look for more your problem OK?

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.textbox1.MaxLength = 2
End Sub
Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(textbox1.Text) OrElse CInt(textbox1.Text) = 0 _
OrElse CInt(textbox1.Text) > 10 Then
MessageBox.Show("Only 1 to 10 is allowed")
textbox1.Focus()
End If
End If
End Sub
Private Sub textbox1_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles textbox1.LostFocus
textbox1.Focus()
End Sub

Cor
 

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

Top