Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only allow numeric values in the textbox

I want to make a TextBox control that only accepts numerical values.

How can I do this in VB6?

like image 983
Gopal Avatar asked Dec 22 '10 04:12

Gopal


People also ask

How do you restrict inputs to only numbers?

To limit an HTML input box to accept numeric input, use the <input type="number">. With this, you will get a numeric input field. After limiting the input box to number, if a user enters text and press submit button, then the following can be seen “Please enter a number.”

How do you allow only numbers when pasted in input text field?

You can simply assign an integer variable to the input widget, it won't allow you to either enter or copy-paste text, It will only allow numbers.


2 Answers

Right click on control box > component > Control -> Microsoft Masked Edit Control 6.0.
Or with normal textbox:

Private Sub Text1_Validate(Cancel As Boolean)
 Cancel = Not IsNumeric(Text1.Text)

End Sub
like image 195
pinichi Avatar answered Sep 23 '22 10:09

pinichi


In the text box text Change event, check if the entered value is a number. If it's not a number then set the old value back again.

Dim textval As String
Dim numval As String

Private Sub TextBox1_Change()
  textval = TextBox1.Text
  If IsNumeric(textval) Then
    numval = textval
  Else
    TextBox1.Text = CStr(numval)
  End If
End Sub
like image 36
Thunder Avatar answered Sep 23 '22 10:09

Thunder