首页 > 代码库 > VB.NET-轻松判断文本框是否为空,一键清空文本框内容

VB.NET-轻松判断文本框是否为空,一键清空文本框内容

    房收费系统的时候,令人相当恶心的一件事就是判断文本框和组合框是否为空还有清空文本框。基本上每个窗体都要进行判断,那一个接着一个的If...Else...语句,长长一串,看着就头疼,但是第一次做机房收费系统的时候竟然傻傻的一个不落都写了出来。真佩服当时的自己,不过这一次还是不要在做那种傻事了,因为我们经历了不少面向对象的洗礼,认识了封装。

         向对象的范畴里,对于相同的或者类似的代码只要重复3次以上我们应该想到面向对象的三大特性之一:封装。想是想到了但是具体怎么通过封装来简化繁琐的判断任务呢?

. 首先新建一个模块,在UI层右键-添加-新建项-选择 Model

              

2. 然后在Model中定义一个结构体Term,且定义一个term类型的结构体数组

          构体(struct)是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构。和基础数据类型Int,Char一样,可以把它定义成自己需要的数据类型。 详情请百度一下。

<span style="font-size:18px;">    '定义结构体Term
    Public 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

    '定义一个term类型的结构体数组
    Public arrayControl() As Term
</span>

. 然后我们就可以在需要的窗体下定义一个过程Rdim() 作用是初始化这个Trem类型的结构体数组。

    <span style="font-size:18px;">Private Sub Rdim()

        ReDim Preserve arrayControl(9)    '重定义数组维数

        '初始化数组
        arrayControl(0) = New Term(txtCID, "卡号")
        arrayControl(1) = New Term(txtBalance, "充值金额")
        arrayControl(2) = New Term(txtSID, "学号")
        arrayControl(3) = New Term(txtName, "姓名")
        arrayControl(4) = New Term(cmbsex, "性别")
        arrayControl(5) = New Term(txtDepartment, "系别")
        arrayControl(6) = New Term(cmbGrade, "年级")
        arrayControl(7) = New Term(txtClass, "班级")
        arrayControl(8) = New Term(cmbType, "类型")
        arrayControl(9) = New Term(cmbstatus, "卡的状态")
    End Sub
</span>


 

. 然后该函数了,先说IsEmptyText()函数,作用是判断文本框、组合框的内容是否为空,为空的话给出相应的提示。它同样写在Model

    ''' <summary>  
    ''' 判断数组中控件的Text属性是否为空,并给出相应提示  
    ''' </summary>  
    ''' <param name="arrayControl">需要遍历的结构体数组</param>  
    ''' <returns>返回Boolean值,true表示为空,false表示不为空</returns>  
    Public Function CheckIsEmpty(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

 

. 一键清除文本框内容的函数,在Model中添加函数AllEmpty()

    ''' <summary>
    ''' 清空窗体中所有文本框、组合框的Text
    ''' </summary>
    ''' <param name="arrayControl">需要遍历的结构体数组</param>
    ''' <returns>返回布尔值,True</returns>
    ''' <remarks></remarks>
    Public Function AllEmpty(ByVal arrayControl() As Term) As Boolean
        Dim termControl As Term                                     '声明一个Term类型变量termControl

        '遍历结构体数组中的所有元素,清空其Text属性
        For Each termControl In arrayControl                        '遍历结构体数组中所有元素
            If TypeOf termControl.controlSub Is TextBox Then        '判断控件是否为文本框
                If termControl.controlSub.Text.Trim <> "" Then    '判断文本框内容是否为空
                    termControl.controlSub.Text = ""
                End If
            ElseIf TypeOf termControl.controlSub Is ComboBox Then   '判断控件是否为组合框 
                If termControl.controlSub.Text.Trim <> "" Then    '判断组合框内容是否为空
                    termControl.controlSub.Text = ""
                End If
            End If
        Next
        Return True
    End Function

 

. 上面说明了这么多,现在到调用的时候了。

检查是否为空:

    Private Sub btnEmpty_Click(sender As Object, e As EventArgs) Handles btnEmpty.Click
        Call Rdim()   ’首先调用Rdim过程进行数组初始化
        '再调用IsEmptyText()函数,检测是否输入文本内容
        If CheckIsEmpty(arrayControl) Then
            Exit Sub
        End If
    End Sub


一键清空:

    Private Sub btnEmpty_Click(sender As Object, e As EventArgs) Handles btnEmpty.Click
        Call Rdim()   ’首先调用Rdim过程进行数组初始化
        '调用清空函数,把窗体中控件的文本清空
        If AllEmpty(arrayControl) Then
            Exit Sub
        End If
    End Sub

其他窗体应用该函数的时候只需要定义一个过程Rdim() 初始化Trem类型的结构体数组,再调用函数即可。

非常方便!随着学习,正在逐渐的触摸到面向对象思想所带来的好处。

 

(PS:不当的地方,恳请指出!)