I am trying to create a retirement calculator. I have finished my design and coded my calculate button. Now I am trying to use the validating event procedue to ensure that data entered into the text boxes is numeric. Here is what I have, the program builds successfully however when non-numeric data is entered I keep getting a nullReferenceException.

System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
at ThomasChantell_Lab4.Form1.txtAge_Validating(Object sender, CancelEventArgs e) in C:\Documents and Settings\TACO\My Documents\Visual Studio 2008\Projects\ThomasChantell-Lab4\ThomasChantell-Lab4\Lab4-ThomasChantell.vb:line 72
at System.Windows.Forms.Control.OnValidating(CancelEventArgs e)
at System.Windows.Forms.Control.NotifyValidating()
at System.Windows.Forms.Control.PerformControlValidation(Boolean bulkValidation)
at System.Windows.Forms.ContainerControl.ValidateThroughAncestor(Control ancestorControl, Boolean preventFocusChangeOnError)

Here is my code:

Private Sub txtAge_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
txtAge.Validating, _
txtYearlySalary.Validating, _
txtRaise.Validating, _
Dim txtBox As TextBox
Dim prompt As String
txtBox = CType(sender, TextBox)
If IsNumeric(txtBox.Text) Then
e.Cancel = False
prompt = "Enter a number in " & txtBox.Tag.ToString
MessageBox.Show(prompt, "Invalid Input: " & txtBox.Tag.ToString, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtBox.Text = ""
e.Cancel = True
End If
End Sub

Welcome itzcrakalakin,
Use code tags to post source code.

What will be the value of Tag property? I think you have to set value of Tag property.

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.