Only accept numerals in imput???

Imfromtexas

Member
Joined
Mar 9, 2005
Messages
8
Programming Experience
Beginner
Im trying to make a set of text box inputs perform calculations, How can I return a msgbox "invalid input" or something of that sort when numerals or symbols are input inplace of a valid number?
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
It depends on when you want to return that message.
You can always check the TextBox.Text property using the Microsoft.VisualBasic.Information.IsNumeric function; either when the text is validated or when you press a button.
You can also handle the keypress event of the textBox and check whether the key being pressed is a number, if not set the KeyPressEventArgs.Handled (e.Handled) to true which will block the processing of said key. You could subclass the textbox class to create a numeric only textbox using this method.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
My solution
VB.NET:
[size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] TextBoxCompanyID_KeyPress([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.KeyPressEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] TextBoxCompanyID.KeyPress

[/size][size=2][color=#008000]'Allows only numeric values into the companyID textbox

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] e.KeyChar.IsNumber(e.KeyChar) = [/size][size=2][color=#0000ff]False[/color][/size][size=2] [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]e.Handled = [/size][size=2][color=#0000ff]True

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff]
[/color][/size]
 

Imfromtexas

Member
Joined
Mar 9, 2005
Messages
8
Programming Experience
Beginner
Im really new at this so thanks for your help. I tried that and that does not allow the use of a decimal or leave the ability to "backspace" in the cell.
Any other suggestions?
Again thanks for your help.
 

Imfromtexas

Member
Joined
Mar 9, 2005
Messages
8
Programming Experience
Beginner
Mabey this will help im trying to have textbox inputs that will send input values to a dynamic array, so really I just need to validate that the input can be used in a mathmatical equation before its sent to the array. Again im new at this so im not sure if im explaining it correcly, thanks
 

Imfromtexas

Member
Joined
Mar 9, 2005
Messages
8
Programming Experience
Beginner
Or maybe thats not even what i need to do, Maybe you can help me with a better direction. I need to have X sets number of inputs, (user driven) and for each set of inputs I have the same formulas to run across each of them. Any Suggestions would be greatly appreciated.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
to elaborate on levyuk's post:

VB.NET:
Private Sub TextBoxCompanyID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxCompanyID.KeyPress

  'Allows only numeric values into the companyID textbox
  If Asc(e.KeyChar) <> Keys.Back And Asc(e.KeyChar) <> Keys.Decimal Then
	If e.KeyChar.IsNumber(e.KeyChar) = FalseThen
	  e.Handled = True
	End If
  End If
End Sub
 
Top Bottom