Hi!
Everyone I am doing one business project in VB.NET... that every form and controls validation are very important, please help me....I am beginner of the VB.NET, suppose to run the form, after finishing data entry to click EXIT button, to show the msgbox if 'yes' close the form, 'no' form never closed...like how i check SAVE,DELETE,EDIT after i am going to click these buttons check the mandatory field.... please help me...VERY URGENT

My advance thanks
Saravanarajan

Dani AI

Generated

A concise plan that ties together the replies from and and gives a maintainable solution for form validation in WinForms.

Use a single validation routine that:

  • Marks required controls (use each control's Tag to flag required fields and provide a friendly name, e.g. Tag = "required:Customer ID").
  • Walks the controls recursively so nested containers (Panel/GroupBox) are validated.
  • Uses an ErrorProvider to show per-control feedback and TryParse for numeric checks (more robust than regex).
  • Returns a list of missing/invalid field names so you can show one aggregated message to the user and set focus to the first invalid control.

Example Validate function and Save usage:

Private ep As New ErrorProvider()

Private Function ValidateForm(ByRef invalidFields As List(Of String)) As Boolean
    invalidFields = New List(Of String)()
    ep.Clear()
    Dim firstInvalid As Control = Nothing

    Dim stack As New Stack(Of Control)()
    stack.Push(Me)
    While stack.Count > 0
        Dim c = stack.Pop()
        For Each child As Control In c.Controls
            stack.Push(child)
            Dim t As String = If(child.Tag, "").ToString()
            If t.ToLower().StartsWith("required") Then
                Dim value As String = If(TypeOf child Is TextBox, CType(child, TextBox).Text.Trim(), "")
                If String.IsNullOrEmpty(value) Then
                    Dim friendly = If(t.Contains(":"), t.Split(":"c)(1), child.Name)
                    invalidFields.Add(friendly)
                    ep.SetError(child, "Required")
                    If firstInvalid Is Nothing Then firstInvalid = child
                End If
            End If
            If t.ToLower().Contains("numeric") AndAlso TypeOf child Is TextBox Then
                Dim txt = CType(child, TextBox).Text.Trim()
                Dim d As Decimal
                If Not Decimal.TryParse(txt, d) Then
                    invalidFields.Add("Invalid number: " & child.Name)
                    ep.SetError(child, "Enter a valid number")
                    If firstInvalid Is Nothing Then firstInvalid = child
                End If
            End If
        Next
    End While

    If firstInvalid IsNot Nothing Then firstInvalid.Focus()
    Return invalidFields.Count = 0
End Function

' On Save:
Dim errs As List(Of String) = Nothing
If Not ValidateForm(errs) Then
    Dim msg = "Please fix the following:" & Environment.NewLine & String.Join(Environment.NewLine, errs)
    MessageBox.Show(msg, "Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    Return
End If
' proceed to save

Notes and pitfalls:

  • Buttons that should bypass validation (like Cancel) must have CausesValidation = False.
  • To prevent closing when validation fails, handle FormClosing and set e.Cancel = True as needed.
  • Prefer Decimal.TryParse/Integer.TryParse or masked controls over regex for numeric/date validation.
  • See the ErrorProvider and Validating event docs for details: ErrorProvider and Control.Validating.

Recommended Answers

All 3 Replies

i am not sure if i understand you .
any way to check mandatory fields , check they not empty
for example if user must enter ID in textbox check it like this

if Id_textbox="" then 
Label1.Text = "you must enter the ID"
end if

about confirming exit the form use msgbox like this

Dim c As Integer
c = MsgBox("are you sure you want to exit", MsgBoxStyle.OKCancel, "Exit")
        If c = 1 Then
            Me.Close()
        End If

its only working exit button query.....plz understand my problem but if click save button at the time to check all the mandatory field and also to show all the field in list to user with msg, "you must enter these fields.....plese help me

regds,
saravanarajan

Hi u can do like this

If Me.Txt_Width.Text = "" Then
MessageBox.Show("Please Enter Width Information")
Me.Txt_Width.Focus()

ElseIf Me.Txt_Width.Text.Length > 5 Then
MessageBox.Show("Please Enter Length Less Than 5")
Me.Txt_Width.Focus()

ElseIf Not Me.regex_Num.IsMatch(Me.Txt_Width.Text) Then
MessageBox.Show("Please Enter Only Digits")
Me.Txt_Width.Focus()

ElseIf Me.Txt_Length.Text = "" Then
MessageBox.Show("Please Enter Length Information")
Me.Txt_Length.Focus()

ElseIf Me.Txt_Length.Text.Length > 5 Then
MessageBox.Show("Please Enter Length Less Than 5")
Me.Txt_Width.Focus()

ElseIf Not Me.regex_Num.IsMatch(Me.Txt_Width.Text) Then
MessageBox.Show("Please Enter Only Digits")
Me.Txt_Length.Focus()

ElseIf Me.Txt_Nearest_Location.Text = "" Then
MessageBox.Show("Please Enter Nearest Location Information")
Me.Txt_Nearest_Location.Focus()

ElseIf Me.Txt_Nearest_Location.Text.Length > 100 Then
MessageBox.Show("Please Enter Length Less Than 100")
Me.Txt_Nearest_Location.Focus()

ElseIf Me.Txt_Code.Text = "" Then
MessageBox.Show("Please Enter Board Code Information")
Me.Txt_Code.Focus()

ElseIf Me.Txt_Map_Reference.Text = """" Then
MessageBox.Show("Please Enter Board Map Reference Information")
Me.Txt_Code.Focus()

ElseIf Not regex_Num.IsMatch(Me.Txt_Code.Text) Then
MessageBox.Show("Please Enter Number In Correct Format")
Me.Txt_Code.Focus()

ElseIf Me.Txt_Map_Reference.Text = "" Then
MessageBox.Show("Please Enter Map Reference information")
Me.Txt_Map_Reference.Focus()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.