Click here to Skip to main content
15,888,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So far I have written code to remain Save and Update buttons disabled until user fills all required fields(Textboxes, Comboboxes) in Groupbox, but I also want the Save and Update buttons to remain disabled until user has addressed all errors available in the form e.g txtISBN.text should not go beyond 13 characters!

I would really appreciate if someone help me in this!

Below is the code that I tried to do so what I have mentioned above but somehow it is not working


VB
Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
'creating a FOR EACH LOOP to check all textboxes for value presence
        For Each t As TextBox In GroupBox1.Controls.OfType(Of TextBox)()
            AddHandler t.TextChanged, AddressOf ValidateInputs
        Next t
        'Also adding below handlers for comboxs to check for value change
        AddHandler cboStaff_id.TextChanged, AddressOf ValidateInputs
        AddHandler cboPub_id.TextChanged, AddressOf ValidateInputs
        AddHandler cboSub_Code.TextChanged, AddressOf ValidateInputs
        AddHandler DateTimePicker1.TextChanged, AddressOf ValidateInputs
    End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
 'creating a FOR EACH LOOP to check all comboboxes for value presence
        For Each cbo As ComboBox In GroupBox1.Controls.OfType(Of ComboBox)()
            AddHandler cbo.TextChanged, AddressOf ValidateInputs
        Next cbo
        'Also adding below handlers for textboxes to check for value change
        AddHandler txtbook_name.TextChanged, AddressOf ValidateInputs
        AddHandler txtauthor.TextChanged, AddressOf ValidateInputs
        AddHandler txtprice.TextChanged, AddressOf ValidateInputs
        AddHandler txtrack_no.TextChanged, AddressOf ValidateInputs
        AddHandler TxtNo_of_Books.TextChanged, AddressOf ValidateInputs
        AddHandler txtvol_no.TextChanged, AddressOf ValidateInputs
        AddHandler DateTimePicker1.TextChanged, AddressOf ValidateInputs
    End Sub
Private Sub ValidateInputs(ByVal Sender As Object, ByVal e As EventArgs)
    Dim ctrl As Control
    Dim strErrorList As String
    strErrorList = ""
    For Each ctrl In Me.Controls
        If Len(ErrorProvider1.GetError(ctrl)) > 0 Then
            strErrorList += ErrorProvider1.GetError(ctrl) & ChrW(10) &
            ChrW(13)
        End If
    Next
    If Len(strErrorList) = 0 Then
        ' Process stuff if no errors
        btnsave.Enabled = Not GroupBox1.Controls.OfType(Of TextBox).Any(Function(t) t.Text = String.Empty) And 
       Not (cboStaff_id.Text = String.Empty OrElse cboPub_id.Text = String.Empty OrElse cboSub_Code.Text = String.Empty _
            OrElse DateTimePicker1.Text = " ")
        btnSaveUpdate.Enabled = Not GroupBox1.Controls.OfType(Of ComboBox).Any(Function(cbo) cbo.Text = String.Empty) And _
            Not (txtbook_name.Text = String.Empty OrElse txtauthor.Text = String.Empty OrElse txtprice.Text = String.Empty _
                 OrElse txtrack_no.Text = String.Empty OrElse TxtNo_of_Books.Text = String.Empty OrElse txtvol_no.Text = String.Empty OrElse DateTimePicker1.Text = " ")
        btndelete.Enabled = Not (cboISBN.Text = String.Empty)
    Else
        btnsave.Enabled = False
        btnSaveUpdate.Enabled = False
        MessageBox.Show(strErrorList, "List Of Errors")
    End If
End Sub
Posted
Comments
CHill60 11-Dec-15 3:44am    
What do you mean by "somehow it is not working" - what is / is not happening?
Have you tried debugging it?
Hazmat HN 11-Dec-15 5:02am    
Above code enables save/update button When I still have error in errorprovider this should not happen!!!

1 solution

Assigning the handlers within the button methods doesn't look right to me - you could add them multiple times and if those buttons are disabled then you'll never validate the inputs.

Here I present a simple form with two textboxes and a button. The button is disabled at the outset and only enabled when both textboxes have something in them
VB.NET
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler TextBox1.TextChanged, AddressOf AllComplete
        AddHandler TextBox2.TextChanged, AddressOf AllComplete
        btnInsert.Enabled = False
    End Sub
    Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        If String.IsNullOrWhiteSpace(TextBox1.Text.Trim()) Then
            e.Cancel = True
        End If
    End Sub
    Private Sub TextBox2_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
        If String.IsNullOrWhiteSpace(TextBox2.Text.Trim()) Then
            e.Cancel = True
        End If
    End Sub
    Private Sub AllComplete(ByVal sender As Object, ByVal e As EventArgs)
        Dim validated As Boolean = Me.ValidateChildren()
        btnInsert.Enabled = validated
    End Sub
End Class

The key points here are:
- The checking for all of the controls being complete happens on the TextChanged event - thus the user must have at least attempted to enter some data before the button is enabled
- The ValidateChildren()[^] function will call all of the Validating events on the form (so don't call the AllComplete method from any Validating event!)
- If the user goes back to a control and deletes the contents after the button has been enabled it is disabled again.

I haven't used an ErrorProvider in this example because all that is doing is highlighting the control that is incorrect and adds nothing to the demonstration of the principle used here.

It is also worth noting that you can't close the form until data has been entered into all of the controls tested. You can get around that by adding the following code
VB.NET
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    e.Cancel = False
End Sub
More information on that problem can be found in (my tip) Allow Form to close when invalid data is present[^] but all you need is the code snippet above
 
Share this answer
 
Comments
Hazmat HN 11-Dec-15 5:19am    
@CHill60 I understood what you have written and my code is doing it fine so far! What I want is that if any field is having wrong input for ex: inserting character in a field that only allows number which triggers error in errorprovider, now at this time I don't want the user to be able to save or update data untill those wrong or invalid inputs are corrected!
F. Xaver 11-Dec-15 5:52am    
where is the problem? Just change the validating of that Textbox to check that the input is an valid Integer for example.
Or better, don't let the user even enter invalid characters, using KeyPress Event to not handling unwanted character inputs
CHill60 11-Dec-15 6:07am    
I gave a simplified version of exactly how to achieve that - the Save and Update buttons can be disabled until all of the controls pass the validation.
The problem with your current code is that the Validation is ONLY done when the user pushes one of the buttons - which they can't do if the buttons are disabled.
Just flesh out my trivial example with your own validations - you can still use the ErrorProvider

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900