机房收费系统总结之4——VB.NET 轻松解决判断文本框、组合框为空问题

来源:互联网 发布:淘宝网狗狗衣服 编辑:程序博客网 时间:2024/04/30 05:25

        纵观机房收费系统,判断文本框、组合框为空问题无非两种情况。第一种:判断窗体中所有文本框、组合框是否为空。第二种:判断一部分文本框、组合框是否为空。下面看看是如何实现这两种情况的。


        第一种:判断窗体中所有文本框、组合框是否为空。

''' <summary>  ''' 判断窗体中所有文本框、组合框输入内容是否为空,若窗体中有允许为空的文本框或组合框,  '''则不能使用此函数  ''' </summary>  ''' <param name="frm"></param>  ''' <returns></returns>  ''' <remarks></remarks>  Public Shared Function IsAllEmptyText(ByVal frm As Form) As Boolean    Dim control As New Control    For Each control In frm.Controls                                '遍历窗体中所有的控件          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 True                Exit Function            End If        End If    Next    Return FalseEnd Function

        第二种:判断一部分文本框、组合框是否为空。

''' <summary>  ''' 判断控件数组中的控件的Text属性是否为空  ''' </summary>  ''' <param name="arrayControl"></param>  ''' <returns></returns>  ''' <remarks></remarks>  Public Shared 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 True                Exit Function            End If        End If    Next    Return FalseEnd Function

        调用函数:

 Dim arrayControl() As Control   ReDim Preserve arrayControl(1)   arrayControl(0) = txtUserName   arrayControl(1) = txtPassword   If UIEmpty.IsSomeEmptyText(arrayControl) Then       Exit Sub   End If 

        按上面执行的话,发现存在些问题
        1.此方法遍历时是从后向前弹出对话框。比如:用户名和密码都为空,它会先弹出密码不能为空,再弹出用户名不能为空。我们如果采用此方法,在放置控件时需注意一下顺序。
        2.控件不能跨容器遍历。比如控件不在一个容器内,有在form体中的,也有在GroupBox中的,就得分别单独判断!



        为此自己进行了一下改进,但相应的麻烦了一些。
        自己定义了一个结构体数组,把控件和对应的文本框信息(如:学号、姓名……)进行封装,遍历到相应的控件,如果为空,就可以有对应的提示。


        1.定义一个结构体数组:

    '定义结构体Term    Private Structure Term        Dim controlSub As Control        Dim strText As String        Sub New(ByVal controlSub As Control, ByVal strText As String)            With Me                .controlSub = controlSub                .strText = strText            End With        End Sub    End Structure

        2.定义一个该类型的结构体数组,并初始化:

        '定义一个term类型的结构体数组        Dim arrayControl() As Term        ReDim Preserve arrayControl(7)        '初始化数组        arrayControl(0) = New Term(txtCardNo, "卡号")        arrayControl(1) = New Term(txtStudentNo, "学号")        arrayControl(2) = New Term(txtName, "姓名")        arrayControl(3) = New Term(comboSex, "性别")        arrayControl(4) = New Term(txtDepName, "系别")        arrayControl(5) = New Term(comboGrade, "年级")        arrayControl(6) = New Term(txtClass, "班级")        arrayControl(7) = New Term(txtMoney, "金额")

        3.调用函数进行判断:

        '调用IsSomeEmptyText()函数,检测是否输入文本内容        If IsSomeEmptyText(arrayControl) Then            Exit Sub        End If

        函数内容:

    ''' <summary>      ''' 判断控件数组中的控件的Text属性是否为空,并进行相应提示      ''' </summary>      ''' <param name="arrayControl">需要遍历的结构体数组</param>      ''' <returns>Boolean值,true表示为空,false表示不为空</returns>      Private Function IsSomeEmptyText(ByVal arrayControl() As Term) As Boolean        Dim termControl As Term                                     '声明Term类型变量termControl        '遍历结构体数组中所有元素,如结构体中的控件文本为空,则找其对就的字符串进行相应提示          For Each termControl In arrayControl                        '遍历结构体数组中所有元素            If TypeOf termControl.controlSub Is TextBox Then        '判断控件是不是文本框                If termControl.controlSub.Text.Trim = "" Then       '判断文本框内容是否为空                    MessageBox.Show(termControl.strText & "不能为空", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)                    termControl.controlSub.Focus()                    Return True                    Exit Function                End If            ElseIf TypeOf termControl.controlSub Is ComboBox Then   '判断控件是不是组合框                 If termControl.controlSub.Text.Trim = "" Then       '判断文本框内容是否为空                    MessageBox.Show(termControl.strText & "不能为空", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)                    termControl.controlSub.Focus()                    Return True                    Exit Function                End If            End If        Next        Return False    End Function


        综上所述,所改进的方法,略显麻烦,但通过手动写入内容,显示时还是蛮灵活的。

        1.不受任何容器的限制。

        2.控件项随意显示。不需要显示的,像只读文本框“充值余额”,就完全不用搭理他。

        3.顺序随意。结构体数据初始化,给他个什么顺序,就会按要求乖乖显示。


原创粉丝点击