循环遍历窗体控件

        在做界面的时候,有的时候需要判断控件是否为空,如果窗体就有一个需要判断那无所谓,直接写一个函数调用就行。可是有的时候窗体中需要判断很多控件,比如说注册时那么多的信息都需要判断,还有就是组合查询一类的等等一些信息,这时候再用调用函数就显得异常麻烦了,因为每一个都需要进行判断,这得重复很多遍。

当然有问题就有解决方法。因为判断的控件都是来自一个窗体,所以只需要编写一个函数,循环遍历窗体的每一个控件就行。具体如下:

<span style="color:#009900;">    ''' <summary>
    ''' 判断窗体中所有控件是否为空
    ''' </summary>
    ''' <param name="frm">窗体变量</param>
    ''' <returns>为空返回True,不为空返回False</returns>
    ''' <remarks></remarks></span>
    Public Function IsAllEmpty(ByVal frm As Form) As Boolean

        Dim control As New Control
        For Each ct1 As Control In frm.Controls
            If ct1.GetType() Is GetType(TextBox) Then
                If ct1.Text.Length = 0 Then
                    MsgBox("信息不完整,请把信息填写完整")
                    ct1.Focus()
                    Return True
                    Exit Function
                End If
            ElseIf ct1.GetType Is GetType(ComboBox) Then
                If ct1.Text.Length = 0 Then
                    MsgBox(ct1.Tag.ToString + "不能为空!")
                    ct1.Focus()
                    Return True
                    Exit Function
                End If

            End If
        Next
        Return False

    End Function



<span style="color:#009900;">    ''' <summary>
    ''' 判断部分控件是否为空
    ''' </summary>
    ''' <param name="arrayCt1">控件集合</param>
    ''' <returns>为空返回True,不为空返回False</returns>
    ''' <remarks></remarks></span>
    Public Function SomeIsEmpty(ByVal arrayCt1() As Control) As Boolean
        Dim control As New Control
        For Each ct1 As Control In arrayCt1
            If ct1.GetType() Is GetType(TextBox) Then
                If ct1.Text.Length = 0 Then
                    MsgBox(ct1.Tag.ToString + "不能为空!", vbOK, "提示信息")
                    ct1.Focus()
                    Return True
                    Exit Function
                End If
            ElseIf ct1.GetType() Is GetType(ComboBox) Then
                If ct1.Text.Length = 0 Then
                    MsgBox(ct1.Tag.ToString + "不能为空!", vbOK, "信息提示")
                    ct1.Focus()
                    Return True
                    Exit Function
                End If
            End If
        Next

        Return False
    End Function

你可能感兴趣的:(function,遍历,控件)