clearing a form for re-entry

Hi all,

Vb Newbie!

I Have an application i am creating for which when i click a clear button the form textboxes are cleared for the next entry. My problem is that i have put the following code into my text_changed text box validations and therefore when clearing the form the below code is generated. Is there a way to disable this when my form clear is fired or is there a better way of validating user input that will not be fired when i clear the form.

Private Sub txtCommAnnSal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCommAnnSal.TextChanged

' Check the flag to prevent code re-entry.

If flag = False Then

' Set the flag to True to prevent re-entry of the code below.

flag = True

' Determine if the text of the control is a number.

If IsNumeric(txtCommAnnSal.Text) = False Then

' Display a message box and clear the contents if not a number.

MessageBox.Show("The text is not a valid number. Please re-enter")

' Clear the contents of the text box to allow re-entry.

txtCommAnnSal.Clear()

End If

' Reset the flag so other TextChanged events are processed correctly.

flag = False

End If

End Sub

I am using textbox.clear to clear the contents of my form for re-entry

any advice greatly appreciated.



Answer this question

clearing a form for re-entry

  • GypsyJazzMan

    Your validation code in general needs improvement. You're going to show a textbox whenever a character is pressed

    At the least, you should only show a message box if the textbox is not empty. A better way would be to handle the lostfocus event, and then provide a message if the box is empty, or not a number. Better yet would be to handle the keypress event and if Char.IsDigit and Char.IsControl both fail, set e.Handled to true so the user can only type digits to start with

    If your format is specific ( such as being 4 digits long ) then the masked text box control is an even better option.



  • Studying

    actually. . .you need to use the validating event. your reset button needs to have CausesValidation set to false.

    If you have three textboxes that need to force numerics -

    Private Sub TextBox1_Validating(ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs) _
    Handles TextBox1.Validating, _
    TextBox2.Validating, _
    TextBox3.Validating

    If TypeOf sender Is TextBoxBase Then
    Dim tb As TextBoxBase = sender
    e.Cancel =
    Not IsNumeric(tb.Text)
    If Not e.Cancel Then Return
    tb.SelectAll()
    MessageBox.Show(
    "The text is not a valid number. Please re-enter")
    End If

    End Sub


  • clearing a form for re-entry