How To Program Textbox Input Event?

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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.
 

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