My program that l am making l need to let the user put anything into a text box but if it has any letters in it to bring up a error message saying that they can only input numbers into it. Anyone able to help me.
I dont know if you were helped already but, if you want it so the user can only enter numbers, or an error message alerts them, then use AsNumeric(intYourChoice)
ex:
If AsNumeric(inEnter) is false then frmError.visible = true End If
Anyways, I have a question of my own, and if you can help, or anyone else Id be thankful. Im trying to make a program that can record your key movements. Im making a calculator program that is always on top, but I want it so that it can lose focus, but still enter the numbers you type, so, that way, if your entering numbers in another program, it will also enter them into the calculator, so you can solve the problem quicker.. ex, my bro plays runescape, and hes always entering numbers onto RS, but he doesnt like having to pull up the calc that comes with the computer everytime. thank you
Sorry, it is KeyChar. I thought it wasn't just Key. Normally I would have checked, but I was flat out. Anyhow, one correction ( as I get the impression the person asking will copy and paste this without reference to my prior comments )
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
e.Handled =
True
MessageBox.Show(
"enter numbers only")
End If
End Sub
Otherwise, pressing 'Delete' or an arrow will give the error message.
OK, you put it in the wrong one both times, VBA is different to VB2005. I've put it in the VB Express area, which is for more beginner level questions.
Basically, if you go to the properties for your textbox, click the lightning bolt to see events, one is called KeyPressed. Type a method name next to KeyPressed to get an event that is called when a key is pressed on your text box. The code probably needs a little massaging, but is essentially correct.
I'm sorry, you've asked this question in the VBA and C++ forums, what language are you using VB 2005 Please confirm so I can move the question to the right place. I'm deleting the other posts.
And the answer is to handle the KeyPressed event and do something like
if Not (Char.IsControl(e.Key) or Char.IsDigit(e.Key)) then e.Handled = true
I'm not sure if it's e.Key, but hte key that's pressed is passed into the event, for sure. This will allow only numbers, to show a message box ( which is user hostile ), just add the code inside a block that also sets e.Handled to true.
did you try what intelllisense suggested for you you can't remember every thing but intellisense help you in this , it will be like this
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsNumber(e.KeyChar) Then
Only allow numbers VB 2005
MazterDeath
I dont know if you were helped already but, if you want it so the user can only enter numbers, or an error message alerts them, then use AsNumeric(intYourChoice)
ex:
If AsNumeric(inEnter) is false then
frmError.visible = true
End If
Anyways, I have a question of my own, and if you can help, or anyone else Id be thankful. Im trying to make a program that can record your key movements. Im making a calculator program that is always on top, but I want it so that it can lose focus, but still enter the numbers you type, so, that way, if your entering numbers in another program, it will also enter them into the calculator, so you can solve the problem quicker.. ex, my bro plays runescape, and hes always entering numbers onto RS, but he doesnt like having to pull up the calc that comes with the computer everytime. thank you
mohammad mehdi arvan
Sorry, it is KeyChar. I thought it wasn't just Key. Normally I would have checked, but I was flat out. Anyhow, one correction ( as I get the impression the person asking will copy and paste this without reference to my prior comments )
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar) Thene.Handled =
TrueMessageBox.Show(
"enter numbers only") End If End SubOtherwise, pressing 'Delete' or an arrow will give the error message.
Husk60
OK, you put it in the wrong one both times, VBA is different to VB2005. I've put it in the VB Express area, which is for more beginner level questions.
Basically, if you go to the properties for your textbox, click the lightning bolt to see events, one is called KeyPressed. Type a method name next to KeyPressed to get an event that is called when a key is pressed on your text box. The code probably needs a little massaging, but is essentially correct.
pascalbjk
I'm sorry, you've asked this question in the VBA and C++ forums, what language are you using VB 2005 Please confirm so I can move the question to the right place. I'm deleting the other posts.
And the answer is to handle the KeyPressed event and do something like
if Not (Char.IsControl(e.Key) or Char.IsDigit(e.Key)) then e.Handled = true
I'm not sure if it's e.Key, but hte key that's pressed is passed into the event, for sure. This will allow only numbers, to show a message box ( which is user hostile ), just add the code inside a block that also sets e.Handled to true.
aepearson
hi,
did you try what intelllisense suggested for you you can't remember every thing but intellisense help you in this , it will be like this
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Not Char.IsNumber(e.KeyChar) Thene.Handled =
TrueMessageBox.Show(
"enter numbers only") End If End Subhope this helps
E. J.
I need it for VB 2005 l put it in the wrong one in the start sorry.
I need more detail then that if posible l dont really understand that, would l just copy and paste it into my program or what
Ed James