VB.NET- Combobox Validation using ErrorProvider

0

Hello good afternoon everyone, along with hello I wanted to ask you, someone has used the errorprovider in the validation of a combobox?

I have a button that in the click event asks to validate that a value is entered in a textbox and that an item of the combobox is selected, in my botin I have this code:

If Me.ValidateChildren And txtNombre.Text <> String.Empty And cmbDepartamento.SelectedIndex.Equals(-1) Then

            'ACCIONES A SEGUIR PORQUE SE PASO LA VALIDACION
            MsgBox("PASAMOS LAS VALIDACIONES")
        Else
            'FALLO LA VALIDACION DE LOS CONTROLES
            MsgBox("SE PRESENTARON ERRORES, FAVOR CORRIGALOS")

        End If

I am using the validating method of the combobox to validate, in which I use this code:

If (cmbDepartamento.SelectedIndex.Equals(0)) Then
    Me.erpSoporte.SetError(sender, "")
Else
   Me.erpSoporte.SetError(sender, "Seleccione Departamento")
End If

but it throws me every time I have to select a value, that is, it recognizes that it does not select anything, but when I select it, it keeps throwing me the same error.

Greetings to everyone and thank you for your time.

    
asked by Nicolas Ezequiel Almonacid 26.04.2018 в 20:15
source

2 answers

0

I do not program in VB.NET but I think something like this could work.

Private Sub TextBox_Validating( sender As System.Object,  e As System.ComponentModel.CancelEventArgs) Handles txtNombre.Validating, cmbDepartamento.Validating
    Dim ctl As Control = CType(sender, Control)
    If ctl.Text = ""
        e.Cancel = True
        erpSoporte.SetError(ctl,"SE PRESENTARON ERRORES, FAVOR CORRIGALOS")
    End If
End Sub

Then you can call it:

erpSoporte.Clear()
If Me.ValidateChildren()
     ' continue aquí'
End If

Finally, if you do not want to write all the controls, you can do this in the form load:

For Each c As Control In Me.Controls
    If TypeOf(c) is TextBox or TypeOf(c) is ComboBox
        AddHandler c.Validating, AddressOf Me.TextBox_Validating
    End If
Next

I hope it helps you.

    
answered by 26.04.2018 в 20:49
0

What is causing you the "Error" is because in your comparison you are indicating that if your index = 0 then do not show the error, otherwise you must show it.

Change it with this to see if it works for you

Private Sub ComboBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cmbDepartamento.Validating
    If cmbDepartamento.SelectedIndex.Equals(-1) Then
        Me.erpSoporte.SetError(sender, "Seleccione Departamento")
    Else
        Me.erpSoporte.SetError(sender, "")
    End If
End Sub
    
answered by 26.04.2018 в 20:28