判断文本框是否为空

在窗体上往往有很多文本框需要输入信息,一些下拉框需要选择,对于这些信息的输入,我们总是需要判断输入的是否为空,以前,总是一个一个的判断,这样太繁琐,也可能会丢掉其中的一个两个的。现在就让我们轻松解决判断文本框是否为空吧。

<span style="font-size:18px;">''' <summary>
''' 用来判断文本框和下拉框是否为空
''' </summary>
''' <remarks></remarks>
Module Module1
    Public Function IsSomeEmptyText(ByVal arrayControl() As Control) As Boolean
        Dim control As New Control

        For Each control In arrayControl '遍历数组中的所有元素
            If TypeOf control Is TextBox Then '判断控件是不是文本框
                If control.Text.Trim = "" Then  '判断文本框内容是不是为空
                    MsgBox(control.Tag.ToString + "不能为空", vbOKOnly, "温馨提示")
                    control.Focus()
                    Return True
                    Exit Function
                End If
            ElseIf TypeOf control Is ComboBox Then '判断控件是不是组合框
                If control.Text.Trim = "" Then
                    MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
                    Return False
                    Exit Function
                End If
            End If
        Next
        Return False
    End Function
End Module
</span>


调用函数:

 <span style="font-size:18px;">Dim arrayControl() As Control
        ReDim Preserve arrayControl(5)

        arrayControl(0) = txtHourRate
        arrayControl(1) = txtHourTmpRate
        arrayControl(2) = txtLeastTime
        arrayControl(3) = txtLeastM
        arrayControl(4) = txtIncreaseTime
        arrayControl(5) = txtPrepareTime

        If IsSomeEmptyText(arrayControl) Then
            Exit Sub
        End If</span>

注意:
<span style="font-size:18px;"> MsgBox(control.Tag.ToString + "不能为空", vbOKOnly, "温馨提示")</span>
其中的 Tag 属性往往忘记赋值,通常赋值为文本框前面的label.text

评论 41
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值