加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

判断文本框是否为空

发布时间:2020-12-17 07:54:24 所属栏目:百科 来源:网络整理
导读:在窗体上往往有很多文本框需要输入信息,一些下拉框需要选择,对于这些信息的输入,我们总是需要判断输入的是否为空,以前,总是一个一个的判断,这样太繁琐,也可能会丢掉其中的一个两个的。现在就让我们轻松解决判断文本框是否为空吧。 span style="font-s

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

<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 + "不能为空!","温馨提示")
                    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 + "不能为空","温馨提示")</span>
其中的 Tag 属性往往忘记赋值,通常赋值为文本框前面的label.text

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读