判断文本框、组合框为空太麻烦?

          做机房重构这么长时间了,由纯三层转到加模式加各种其他技术。写了各个层的代码,每次写到要判断文本框或组合框为空的时候总要重复一个一个的判断,虽简单但写的太多了就感觉不怎么爽。不将就是创造的原动力,整点儿技术含量的方法。

      今天就解决了这个问题:判断是否为空就两种情况:1.全部为空2.部分为空

1.全部为空

 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
2.部分为空
 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

      特别提醒:注意每个控件摆放的位置,因为control遍历是从最后放上去的一个控件向前遍历的

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值