How To Program Textbox Input Event?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Using VB express, I have a textbox in a form and want to ask the user to
input a number , If the number is 114, then I want to do a form2.show
/me.hide so the form2 comes up, but if they put in the wrong number then a
messagebox comes up and says, you entered a wrong number. Can anyone help me
with this code?
Thanks,
Jerry
 
In the Leave event handler of the text box:

Dim f2 As Form2
If (TextBox1.Text = "114") Then
f2 = New Form2()
Me.Hide()
f2.Show()
Else
MessageBox.Show ("Invalid Number")
End If
 
I don't get this, I want to leave the form to another form if I put 114 in
the textbox and press the enter key.
Thanks,
Jerry
 
The following code works, but where do I put the messagebox.show statement so
it still works?
Thanks,
Jerry

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If TextBox1.Text = "114" Then
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
mcc114.Show()
Me.Hide()
End If

End If
End Sub
 
Code needs minor modification (swap the If conditions) to accomodate the
MessageBox.Show:

If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
If (TextBox1.Text = "114") Then
mcc114.Show()
Me.Hide()
Else
MessageBox.Show ("Invalid number")
End If
End If
 
This logic will validate the number only after your press the Enter key. It
will not pop up the message box as type in 112.

I tried the code and it does work.
 
Back
Top