Recursive Controls and Error Icon

SonicBoomAu

Centurion
Joined
Oct 30, 2003
Messages
179
Location
Australia
Hi All,

Is it possible to do something like this??

I get a blue line under "ErrorProvider1.SetError(Txt.Name, " and "ErrorProvider1.SetError(Cbo.Name"

The mouse over states "Value of type 'String' cannot be converted to 'System.Windows.Forms.Control"


Visual Basic:
 ' Loop through each control on the StaticGroupBox
        ' If control.text is blank then display error icon on that feild
        Dim Ctrl As Control
        Dim Txt As TextBox
        Dim Cbo As ComboBox

        For Each Ctrl In Me.gbxSecurityInfo.Controls
            If TypeOf (Ctrl) Is TextBox Then
                Txt = CType(Ctrl, TextBox)
                If Txt.Text = vbNullString Then
                    ' Display Error Icon next to control
                    ErrorProvider1.SetError(Txt.Name, "This feild can not be left blank")
                Else
                    ErrorProvider1.SetError(Txt.Name, "")
                End If 'Txt.Text = vbNullString Then
            End If ' TypeOf (CTRL) is TextBox

            If TypeOf (Ctrl) Is ComboBox Then
                Cbo = CType(Ctrl, ComboBox)
                If Cbo.Text = vbNullString Then
                    ' Display Error Icon next to control
                    ErrorProvider1.SetError(Cbo.Name, "This feild can not be left blank")
                Else
                    ErrorProvider1.SetError(Cbo.Name, "")
                End If 'Cbo.Text = vbNullString Then
            End If ' TypeOf (Ctrl) Is ComboBox Then
        Next

BTW i'm using VB .Net 2003

Thanks you for your time and help in advance.
 
Hi SonicBoomAu,

The error is correct as the ErrorProvider accesses the control, not its string name.
Try this instead:
Visual Basic:
        Dim Ctrl As Control
        Dim txtBox As TextBox
        Dim combo As ComboBox

        For Each Ctrl In Me.gbxSecurityInfo.Controls
            If TypeOf Ctrl Is TextBox Then
                txtBox = CType(Ctrl, TextBox)
                If txtBox.Text = String.Empty Then
                    ' Display Error Icon next to control
                    ErrorProvider1.SetError(txtBox, "This field can not be left blank")
                Else
                    ErrorProvider1.SetError(txtBox, "")
                End If 'Txt.Text = String.Empty Then
            End If ' TypeOf CTRL is TextBox

            If TypeOf Ctrl Is ComboBox Then
                combo = CType(Ctrl, ComboBox)
                If combo.Text = String.Empty Then
                    ' Display Error Icon next to control
                    ErrorProvider1.SetError(combo, "This field can not be left blank")
                Else
                    ErrorProvider1.SetError(combo, "")
                End If 'Cbo.Text = String.Empty Then
            End If ' TypeOf Ctrl Is ComboBox Then
        Next
 
Thanks for the prompt reply DiverDan. That fixed that problem, but identified another one.

I wont to loop through all the controls on the groupbox, but am only getting the controls on the form. What am i donig wrong.

Thanks in advance
 
Is gbxSecurityInfo the groupbox that has the controls that you want to loop through?

If so then the code is correct. If not then you'll need to change gbxSecurityInfo to the correct groupbox.

Another thought is; are you sure that the controls are part of gbxSecurityInfo's controls and not the form's? You might want to check that.
 
Back
Top