机房重构-----判断输入框是否为空,一键清除输入框

在机房重构过程中,为避免代码冗余,文章介绍了如何通过封装来判断并清除输入框内容。以注册用户场景为例,定义了Term结构体,创建Model类型,并利用循环数组实现对多个输入框的空值检查和一键清除功能。后续文章将探讨使用正则表达式进一步对输入字符条件进行判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在机房重构中,经常重复遇到的事情就是判断输入框是否为空,并且清除输入框的内容。

当在系统中,遇到同样的代码出现三次及以上时,就要思考将他们封装起来,减少冗余。

所以,利用循环数组的方法,依次判断输入框是否为空,并一键清除输入框内容。

下面以注册用户为例,介绍一下,封装的过程:

首先,先要定义一个Term类型的结构体

结构体是什么?

结构体就是一个可以包含不同数据类型的一个结构,它是一种可以自己定义的数据类型,如同我们遇到的Int,String型。但他有两个特点:首先结构体可以在一个结构中声明不同的数据类型。第二,相同结构的结构体变量是可以相互赋值的,而数组是做不到的,因为数组是单一数据类型的数据集合,它本身不是数据类型(而结构体是)。

下面是对结构体的定义过程

本着封装的原则,前提要新建一个Model类型。

<strong><span style="font-family:KaiTi_GB2312;font-size:18px;"> Public Structure Term '定义一个Term类型结构体。
        Dim controlsub As Control
        Dim txt As String
        Sub New(ByVal controlsub As Control, ByVal txt As String)
            With Me
                .controlsub = controlsub
                .txt = txt
            End With
        End Sub
    End Structure</span></strong>

接下来,是在窗体中,实例化Term类型结构体数组的过程。

<strong><span style="font-family:KaiTi_GB2312;font-size:18px;"> Private Sub Rdim()
        ReDim Preserve arrayControl(9) '重定义数组维数
        '初始化数组
        arrayControl(0) = New Term(txtCardNo, "卡号")
        arrayControl(1) = New Term(txtCash, "充值金额")
        arrayControl(2) = New Term(txtClass, "班级")
        arrayControl(3) = New Term(txtDepartment, "系别")
        arrayControl(4) = New Term(txtGrade, "年级")
        arrayControl(5) = New Term(txtName, "姓名")
        arrayControl(6) = New Term(txtStudentNo, "学号")
        arrayControl(7) = New Term(cmbSex, "性别")
        arrayControl(8) = New Term(cmbState, "状态")
        arrayControl(9) = New Term(cmbStyle, "类型")
    End Sub  </span></strong>
判断输入框是否为空。(同样是在Model中)

<strong><span style="font-family:KaiTi_GB2312;font-size:18px;">  Public arrayControl() As Term
    '判断输入框是否为空
    Public Function CheckIsEmpty(ByVal arrayControl() As Term) As Boolean
        Dim termControl As Term   '声明一个Term类型变量
        For Each termControl In arrayControl
            If TypeOf termControl.controlsub Is TextBox Then
                If termControl.controlsub.Text.Trim() = "" Then
                    MsgBox("输入内容不能为空!")
                    termControl.controlsub.Focus()
                    Return True
                    Exit Function
                End If
            ElseIf TypeOf termControl.controlsub Is ComboBox Then
                If termControl.controlsub.Text.Trim = "" Then       '判断组合框内容是否为空  
                    MsgBox("输入内容不能为空!")
                    termControl.controlsub.Focus()                  '为空控件得到焦点  
                    Return True
                    Exit Function
                End If
        End If  
        Next
        Return False
    End Function</span></strong>
一键清除输入框内容。(同样是在Model中)

<strong><span style="font-family:KaiTi_GB2312;font-size:18px;"> '清除输入框内容
    Public Function MakeEmpty(ByVal arrayControl() As Term) As Boolean
        Dim termControl As Term '声明一个Term类型变量

        For Each termControl In arrayControl
            If TypeOf termControl.controlsub Is TextBox Or TypeOf termControl.controlsub Is ComboBox Then
                termControl.controlsub.Text = ""
            End If
        Next
        Return True
    End Function</span></strong>
最后是在窗体中调用的过程。

<strong><span style="font-family:KaiTi_GB2312;font-size:18px;">  '检查输入框是否为空。
        Call Rdim()
        If CheckIsEmpty(arrayControl) = True Then
            Exit Sub
        End If</span></strong>
<strong><span style="font-family:KaiTi_GB2312;font-size:18px;">
</span></strong><pre name="code" class="csharp"><strong><span style="font-family:KaiTi_GB2312;font-size:18px;">'一键清除文本框内容</span></strong>

<strong><span style="font-family:KaiTi_GB2312;font-size:18px;">Call Rdim()
        If MakeEmpty(arrayControl) Then
            Exit Sub
        End If</span></strong>

总结:

   经过封装之后,利用循环数组就对输入框和选择框是否有内容作出判断。其他窗体,想做判断,只要实例化Term类型结构体(定义数组),然后直接调用模块中的方法即可。减少了一连串的IF.......ELSE 判断语句。在下面一篇文章中,继续进行封装。将介绍“正则表达式”,对输入框的输入字符条件,进行判断。




评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值