循环遍历窗体控件

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

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

<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

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 21
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值