机房重构——清空与判空

【前言】

机房重构的时候会遇到很多清空,判空的地方,下面来总结一下清空判空的办法。

【正文】

如果只有几个地方需要清空,直接写下面的代码就OK了。

txtStudentNo.Text = ""
        txtName.Text = ""
        combSex.Text = ""

可是如果要清空很多的地方,上面那种方法就比较麻烦。

                                                   

可以利用模块来进行清空或者判空

一般都是在U层中进行清空或者判空,所以模块就写在U层,

Module CheckModule
    '在模块中定义结构体Term,并定义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

    '判空  的方法
    Public Function CheckIsEmpty(ByVal arrayControl() As Term) As Boolean
        Dim termControl As Term

        '遍历结构体数组中所有的元素,如果控件文本为空,则进行相应提示
        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  '如果不为空,返回False

    End Function


    '清空所有内容
    Public Function AllEmpty(arrayControl() As Term) As Boolean
        Dim termControl As 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  '如果为空返回 True

    End Function

End Module

然后在需要清空的方法里面写上如下代码

 '初始化term类型的结构体数组
    Private Sub Rdim()
        ReDim Preserve arrayControl(4) '重新定义数组维数
        '初始化数组
        arrayControl(0) = New Term(txtCardNo, "卡号"
                                  )
        arrayControl(1) = New Term(txtStudentNo, "学号")
        arrayControl(2) = New Term(combType, "类型")
        arrayControl(3) = New Term(combStatue, "状态")
        arrayControl(4) = New Term(TxtCash, "充值金额")

    End Sub
    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        '调用模块中的清空方法
        Call Rdim()
        If AllEmpty(arrayControl) Then
            Exit Sub
        End If
    End Sub





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 32
    评论
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值