How to make a text field only accept number

Ok I am writing a program that takes 3 grades and performs the sum of them and compares to 189. If the total sum is higher than 189 it displays the score and pass. If the score is less than 189 it displays the score and fail.

The problem lies in that in the fields where i input test scores it should only take numbers, but the program actually lets me put letters on it. If i put a letter and try to calculate the program crashes. I wanted to see if I could actually make the program so only numbers can be placed in it.

Here is a copy of the source file:

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

Friend WithEvents TextBox2 As System.Windows.Forms.TextBox

Friend WithEvents TextBox3 As System.Windows.Forms.TextBox

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents Button2 As System.Windows.Forms.Button

Friend WithEvents TextBox4 As System.Windows.Forms.TextBox

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Label3 As System.Windows.Forms.Label

Friend WithEvents Label4 As System.Windows.Forms.Label

Friend WithEvents Label5 As System.Windows.Forms.Label

Friend WithEvents Label6 As System.Windows.Forms.Label

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents Label7 As System.Windows.Forms.Label

Friend WithEvents Label8 As System.Windows.Forms.Label

Friend WithEvents TextBox5 As System.Windows.Forms.TextBox

Friend WithEvents Label9 As System.Windows.Forms.Label

Friend WithEvents TextBox6 As System.Windows.Forms.TextBox

Friend WithEvents Label10 As System.Windows.Forms.Label

Friend WithEvents Button3 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.TextBox1 = New System.Windows.Forms.TextBox

Me.TextBox2 = New System.Windows.Forms.TextBox

Me.TextBox3 = New System.Windows.Forms.TextBox

Me.Button1 = New System.Windows.Forms.Button

Me.Button2 = New System.Windows.Forms.Button

Me.TextBox4 = New System.Windows.Forms.TextBox

Me.Label1 = New System.Windows.Forms.Label

Me.Label3 = New System.Windows.Forms.Label

Me.Label4 = New System.Windows.Forms.Label

Me.Label5 = New System.Windows.Forms.Label

Me.Label6 = New System.Windows.Forms.Label

Me.Label2 = New System.Windows.Forms.Label

Me.Label7 = New System.Windows.Forms.Label

Me.Label8 = New System.Windows.Forms.Label

Me.TextBox5 = New System.Windows.Forms.TextBox

Me.Label9 = New System.Windows.Forms.Label

Me.TextBox6 = New System.Windows.Forms.TextBox

Me.Label10 = New System.Windows.Forms.Label

Me.Button3 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'TextBox1

'

Me.TextBox1.Location = New System.Drawing.Point(160, 248)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.TabIndex = 0

Me.TextBox1.Text = ""

Me.TextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center

'

'TextBox2

'

Me.TextBox2.Location = New System.Drawing.Point(160, 288)

Me.TextBox2.Name = "TextBox2"

Me.TextBox2.TabIndex = 1

Me.TextBox2.Text = ""

Me.TextBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center

'

'TextBox3

'

Me.TextBox3.Location = New System.Drawing.Point(160, 328)

Me.TextBox3.Name = "TextBox3"

Me.TextBox3.TabIndex = 2

Me.TextBox3.Text = ""

Me.TextBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center

'

'Button1

'

Me.Button1.BackColor = System.Drawing.Color.FromArgb(CType(128, Byte), CType(255, Byte), CType(128, Byte))

Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Button1.Location = New System.Drawing.Point(80, 376)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(128, 23)

Me.Button1.TabIndex = 3

Me.Button1.Text = "CALCULATE"

'

'Button2

'

Me.Button2.BackColor = System.Drawing.Color.Red

Me.Button2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Button2.Location = New System.Drawing.Point(168, 576)

Me.Button2.Name = "Button2"

Me.Button2.TabIndex = 4

Me.Button2.Text = "EXIT"

'

'TextBox4

'

Me.TextBox4.Location = New System.Drawing.Point(152, 432)

Me.TextBox4.Name = "TextBox4"

Me.TextBox4.ReadOnly = True

Me.TextBox4.Size = New System.Drawing.Size(112, 20)

Me.TextBox4.TabIndex = 5

Me.TextBox4.Text = ""

Me.TextBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center

'

'Label1

'

Me.Label1.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(255, Byte), CType(255, Byte))

Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label1.Location = New System.Drawing.Point(16, 16)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(248, 16)

Me.Label1.TabIndex = 6

Me.Label1.Text = "Group Project Unit 5"

Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Label3

'

Me.Label3.BackColor = System.Drawing.SystemColors.InactiveCaptionText

Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label3.Location = New System.Drawing.Point(16, 424)

Me.Label3.Name = "Label3"

Me.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No

Me.Label3.Size = New System.Drawing.Size(112, 32)

Me.Label3.TabIndex = 8

Me.Label3.Text = "Final Score, PASS/FAIL"

Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Label4

'

Me.Label4.BackColor = System.Drawing.SystemColors.InactiveCaptionText

Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label4.Location = New System.Drawing.Point(16, 248)

Me.Label4.Name = "Label4"

Me.Label4.Size = New System.Drawing.Size(88, 16)

Me.Label4.TabIndex = 9

Me.Label4.Text = "GRADE 1"

Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Label5

'

Me.Label5.BackColor = System.Drawing.SystemColors.InactiveCaptionText

Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label5.Location = New System.Drawing.Point(16, 288)

Me.Label5.Name = "Label5"

Me.Label5.Size = New System.Drawing.Size(88, 16)

Me.Label5.TabIndex = 10

Me.Label5.Text = "GRADE 2"

Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Label6

'

Me.Label6.BackColor = System.Drawing.SystemColors.InactiveCaptionText

Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label6.Location = New System.Drawing.Point(16, 328)

Me.Label6.Name = "Label6"

Me.Label6.Size = New System.Drawing.Size(88, 16)

Me.Label6.TabIndex = 11

Me.Label6.Text = "GRADE 3"

Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Label2

'

Me.Label2.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(255, Byte), CType(255, Byte))

Me.Label2.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label2.Location = New System.Drawing.Point(16, 48)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(248, 48)

Me.Label2.TabIndex = 12

Me.Label2.Text = "Samuel Soto, Joseph Szweb, William Christopher, Mario Sierra, Tracey Ann Walker" & _

""

Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Label7

'

Me.Label7.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(128, Byte), CType(0, Byte))

Me.Label7.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label7.Location = New System.Drawing.Point(16, 112)

Me.Label7.Name = "Label7"

Me.Label7.Size = New System.Drawing.Size(248, 48)

Me.Label7.TabIndex = 13

Me.Label7.Text = "Please enter the grades and click on calculate. The sum of the 3 grades will be c" & _

"ompared to 189."

Me.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Label8

'

Me.Label8.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(128, Byte), CType(0, Byte))

Me.Label8.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label8.Location = New System.Drawing.Point(16, 176)

Me.Label8.Name = "Label8"

Me.Label8.Size = New System.Drawing.Size(248, 48)

Me.Label8.TabIndex = 14

Me.Label8.Text = "A Final score of 189 or higher equals a PASS grade. Anything less equals a FAIL g" & _

"rade."

Me.Label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'TextBox5

'

Me.TextBox5.Location = New System.Drawing.Point(152, 480)

Me.TextBox5.Name = "TextBox5"

Me.TextBox5.ReadOnly = True

Me.TextBox5.Size = New System.Drawing.Size(112, 20)

Me.TextBox5.TabIndex = 15

Me.TextBox5.Text = ""

Me.TextBox5.TextAlign = System.Windows.Forms.HorizontalAlignment.Center

'

'Label9

'

Me.Label9.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(192, Byte), CType(128, Byte))

Me.Label9.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label9.Location = New System.Drawing.Point(8, 480)

Me.Label9.Name = "Label9"

Me.Label9.Size = New System.Drawing.Size(120, 23)

Me.Label9.TabIndex = 16

Me.Label9.Text = "PERCENTILE"

'

'TextBox6

'

Me.TextBox6.Location = New System.Drawing.Point(152, 528)

Me.TextBox6.Name = "TextBox6"

Me.TextBox6.ReadOnly = True

Me.TextBox6.Size = New System.Drawing.Size(112, 20)

Me.TextBox6.TabIndex = 17

Me.TextBox6.Text = ""

Me.TextBox6.TextAlign = System.Windows.Forms.HorizontalAlignment.Center

'

'Label10

'

Me.Label10.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(0, Byte))

Me.Label10.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Label10.Location = New System.Drawing.Point(8, 520)

Me.Label10.Name = "Label10"

Me.Label10.Size = New System.Drawing.Size(128, 40)

Me.Label10.TabIndex = 18

Me.Label10.Text = "GRADE POINT AVERAGE ON A 4.0 SCALE"

Me.Label10.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Button3

'

Me.Button3.BackColor = System.Drawing.Color.Yellow

Me.Button3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.Button3.Location = New System.Drawing.Point(8, 576)

Me.Button3.Name = "Button3"

Me.Button3.Size = New System.Drawing.Size(144, 23)

Me.Button3.TabIndex = 19

Me.Button3.Text = "Program information"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.BackColor = System.Drawing.SystemColors.Highlight

Me.ClientSize = New System.Drawing.Size(292, 614)

Me.Controls.Add(Me.Button3)

Me.Controls.Add(Me.Label10)

Me.Controls.Add(Me.TextBox6)

Me.Controls.Add(Me.Label9)

Me.Controls.Add(Me.TextBox5)

Me.Controls.Add(Me.Label8)

Me.Controls.Add(Me.Label7)

Me.Controls.Add(Me.Label2)

Me.Controls.Add(Me.Label6)

Me.Controls.Add(Me.Label5)

Me.Controls.Add(Me.Label4)

Me.Controls.Add(Me.Label3)

Me.Controls.Add(Me.Label1)

Me.Controls.Add(Me.TextBox4)

Me.Controls.Add(Me.TextBox3)

Me.Controls.Add(Me.TextBox2)

Me.Controls.Add(Me.TextBox1)

Me.Controls.Add(Me.Button2)

Me.Controls.Add(Me.Button1)

Me.Name = "Form1"

Me.Text = "Grade Calculator 1.1"

Me.ResumeLayout(False)

End Sub

#End Region

'declare variables

Dim inputGrade1 As Integer

Dim inputGrade2 As Integer

Dim inputGrade3 As Integer

Dim Results As Integer

Dim resultAverage As Integer

Dim gradePointAverage As Decimal

Dim reply

Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'assign value from form

inputGrade1 = TextBox1.Text

inputGrade2 = TextBox2.Text

inputGrade3 = TextBox3.Text

'perform calculations

Results = inputGrade1 + inputGrade2 + inputGrade3

resultAverage = Results / 3

gradePointAverage = resultAverage * 0.04

'display results

If Results >= 189 Then

TextBox4.Text = (Results & ", Pass")

Else

TextBox4.Text = (Results & ", Fail")

End If

TextBox5.Text = resultAverage

TextBox6.Text = gradePointAverage

End Sub

Private Sub Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

reply = MsgBox("Are you sure you want to close this application ", vbYesNo, "Closing application")

If reply = vbYes Then End

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

End Sub

Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click

End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

End Sub

Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click

End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged

End Sub

Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click

End Sub

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged

End Sub

Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

MsgBox("Thanks for using Grade Calculator 1.1 . We hope you enjoyed this program", MsgBoxStyle.Exclamation, "About Grade Calculator 1.1")

End Sub

End Class



Answer this question

How to make a text field only accept number

  • PaoloQuadrani

    Thanks for the prompt answers, I tried a search, but I was somewhat confused on how to search this type of problem, thanks for helping me tons, i will change the title of the topic so people can search i easier.
  • mtbDad

    1 - I've moved this to the VB Express forum, where it belongs

    2 - you've posted all your code, most of which is generated by the IDE, including a ton of event handlers you've created but do not use. You should try to post only the relevant, bits, and in this case, as your question is not related to existing code, no code sample was needed.

    3 -

    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

    End If

    End Sub

    Assuming your textbox is called TextBox1, of course. I copied and pasted this from an answer I gave yesterday, it's worthwhile searching the forums before asking, just so you get an answer quicker :-)



  • Raj Nair

    If you are using VB 2005 Express Edition, instead of using a regular Textbox, you can use the Masked Textbox and set the Mask to Numeric. That way, your users can only input numbers from 0 to 9. There will be a small Arrow in the upper right portion of the Masked Textbox that you can click and set the MASK. Set it to Numeric and in the bottom poriton of the box you are looking at, you can set the number of characters allowed. ( in the area with the 00000 zeros......add or removed for the number of characters you want) Otherswise, you can do it the old hard way with a regular textbox:

    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp

    If TextBox1.Text Like "*[!0-9]*" Then

    TextBox1.Text = ""

    Exit Sub

    Else

    End If

    End Sub

    This will filter the input to 0 to 9 . Which ever method you use, you will need to convert the Textbox's text to an Integer :

    inPutGrade1 = Cint(Textbox1.Text) Otherwise, it will still treat the characters as strings instead of numbers.

    james

    aka:Trucker


  • How to make a text field only accept number