We help IT Professionals succeed at work.

Error Provider Function in VB 2005

cmdolcet
cmdolcet asked
on
I have placed an error provider on my windows form. along with the error provider I have placed 5 combo boxes. In each select index change I have the code below. The problem I get into is if I change a combo box and it matches one currently selected it triggers the code below and catches the error and the warning. However if I dont fix the error and change a different combo box that error warning goes away.

Is there any way to control this or do I need to add an error provider for each combo box

Thanks in advance.
Private Sub cboPosition4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPosition4.SelectedIndexChanged 

If Me.cboPosition4.Text = Me.cboPosition1.Text Or Me.cboPosition4.Text = Me.cboPosition2.Text Or Me.cboPosition4.Text = Me.cboPosition3.Text Or Me.cboPosition4.Text = Me.cboPosition5.Text Then
            Me.btnSet.Enabled = False
            Me.btnSetDefault.Enabled = False
            Me.SetupError.SetError(Me.cboPosition4, "Please change the duplicate Position type.....")
        Else
            Me.btnSet.Enabled = True
            Me.btnSetDefault.Enabled = True
            Me.SetupError.Clear()
        End If

 End Sub

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Either add this code for all drop downs or generalize this code so that it handles all dropdowns.

Author

Commented:
The code is available for all drop down what happens is that that when you enter another another combo box the error provide gets reset.

Other than adding 5 different error providers to the form is there another way?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
I think a common handler like below should work


Private Sub cboPosition4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPosition1.SelectedIndexChanged, cboPosition2.SelectedIndexChanged , cboPosition3.SelectedIndexChanged cboPosition4.SelectedIndexChanged 

If Me.cboPosition1.Text = Me.cboPosition2.Text Or Me.cboPosition1.Text = Me.cboPosition3.Text Or Me.cboPosition1.Text = Me.cboPosition4.Text Then
            Me.btnSet.Enabled = False
            Me.btnSetDefault.Enabled = False
            Me.SetupError.SetError(Me.cboPosition1, "Please change the duplicate Position type.....")
ElseIf Me.cboPosition2.Text = Me.cboPosition1.Text Or Me.cboPosition2.Text = Me.cboPosition3.Text Or Me.cboPosition2.Text = Me.cboPosition4.Text Then
            Me.btnSet.Enabled = False
            Me.btnSetDefault.Enabled = False
            Me.SetupError.SetError(Me.cboPosition2, "Please change the duplicate Position type.....")

ElseIf ...
        Else
            Me.btnSet.Enabled = True
            Me.btnSetDefault.Enabled = True
            Me.SetupError.Clear()
        End If

 End Sub

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.