如何在我的函数中添加'ErrorProvider1'

时间:2016-11-14 15:46:01

标签: vb.net

我从这个漂亮的网站上学到了很多,但仍然有一个问题,我无法找到答案 如何在我的函数中添加'ErrorProvider1' 这是不行的方式 有什么建议

Public Function checkif_nothing(ByVal frm As Form, controal_colection As Control.ControlCollection) As Boolean 'chick if no valu in text box an combobox in form 
        Dim msg As Boolean = True
        Dim ctrl As Control
        For Each ctrl In controal_colection
            If (ctrl.GetType() Is GetType(TextBox)) Then
                Dim txt As TextBox = CType(ctrl, TextBox)
                If txt.Text = "" Then
                    txt.BackColor = Color.LightYellow
                   frm.ErrorProvider1.SetError(txt, "Please Fill Textbox")
                    msg = False
                End If
            End If
            If (ctrl.GetType() Is GetType(ComboBox)) Then
                Dim comp As ComboBox = CType(ctrl, ComboBox)
                If comp.SelectedItem = Nothing Then
                    comp.BackColor = Color.LightYellow

                    frm.ErrorProvider1.SetError(comp, "Select one Value")
                    msg = False
                End If
            End If
        Next

        Return msg

    End Function

1 个答案:

答案 0 :(得分:0)

我将错误传递给函数并帮助我

 Public Function checkif_nothing(err As ErrorProvider, controal_colection As Control.ControlCollection) As Boolean 'chick if no valu in text box an combobox in form 
        Dim msg As Boolean = True
        Dim ctrl As Control
        For Each ctrl In controal_colection

            If (ctrl.GetType() Is GetType(TextBox)) Then
                Dim txt As TextBox = CType(ctrl, TextBox)
                If txt.Text = "" Then
                    txt.BackColor = Color.LightYellow
                    err.SetError(txt, "Please Fill Textbox")

                    msg = False
                End If
            End If
            If (ctrl.GetType() Is GetType(ComboBox)) Then
                Dim comp As ComboBox = CType(ctrl, ComboBox)
                If comp.SelectedItem = Nothing Then
                    comp.BackColor = Color.LightYellow

                    err.SetError(comp, "Select one Value")
                    msg = False
                End If
            End If
        Next

        Return msg

    End Function