乌龟漫步之判断文本框是否为空/清空文本

       在机房收费系统中,几乎每个窗体都会有文本框的存在。而在一些比如注册,查询的窗体中,我们总会需要判断文本框里的是否存在内容或者把所有文本控件里的不需要的内容全部清除。当存在一两个少数的文本控件时,简单的方法当然就是直接的给判断。可是随着文本控件的增多,单独判断所需的工作量当然会很大,这时就要找寻一种更加简单的方法来使得自己的操作更加的高效。

      当我们选择直接判断时,做的最多的工作肯定就是复制粘贴吧!例如:

 If txtGu.Text = "" Then
            MsgBox("请输入固定用户每小时的费用")
            txtGu.Select()
            txtGu.Focus()
        ElseIf txtLin.Text = "" Then
            MsgBox("请输入临时用户每小时的费用")
            txtLin.Select()
            txtLin.Focus()
        ElseIf txtIncrease.Text = "" Then
            MsgBox("请输入递增的时间")
            txtIncrease.Select()
            txtIncrease.Focus()
        ElseIf txtLeast.Text = "" Then
            MsgBox("请输入至少上机时间")
            txtLeast.Select()
            txtLeast.Focus()
        ElseIf txtPrepare.Text = "" Then
            MsgBox("请输入准备时间")
            txtPrepare.Select()
            txtPrepare.Focus()
        ElseIf txtLeastMoney.Text = "" Then
            MsgBox("请输入准备时间")
            txtLeastMoney.Select()
            txtLeastMoney.Focus()
      上面所举的例子是我们刚接触VB的时候,不懂得面向对象的直接的做法。而现在的我们学会的对内容进行封装,从而可以被重复的使用。

Imports System.Windows.Forms

Module CheckText

    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
    Public arrayControl() As Term    '定义一个Term类型的结构体数组
    '判断窗体控件是否为空
    Public Function IsEmpty(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
    End Function
    '全部清空
    Public Function EmptyAll(ByVal arrayControl() As Term) As Boolean
        Dim termControl As Term
        For Each termControl In arrayControl
            If TypeOf termControl.controlSub Is TextBox Then
                termControl.controlSub.Text = ""
                termControl.controlSub.Focus()
            ElseIf TypeOf termControl.controlSub Is ComboBox Then
                termControl.controlSub.Text = ""
                termControl.controlSub.Focus()

            End If
        Next
        Return True
    End Function
End Module
  当窗体需要用到这种方法时,只要直接重定义,给予相应的参数,并调用相应的方法即可。例如

</pre><pre name="code" class="csharp">Public Class frmPassword
    Private Sub Rdim()
        ReDim Preserve arrayControl(2)              '重定义数组维数
        arrayControl(0) = New Term(txtOld, "旧密码")
        arrayControl(1) = New Term(txtNew, "新密码")
        arrayControl(2) = New Term(txtenNew, "确认口令")
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Call Rdim()
        If IsEmpty(arrayControl) Then           ’如果要清空,则直接调用EmptyAll(arrayControl)
            Exit Sub
        End If

       当所有的事有多次重复的时候,我们就应该给自己找一个“ ”的理由。就比如一条路以前都是靠人们走,后来人们懒得走,觉得累,就训练了马背着自己走,后来又发明了自行车,汽车,火车,动车,飞机。。。。人们懒的前途是不可限量地。所以在我们设计程序时在适当的时候做适当的偷懒会提高我们的效率哟!!!

    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 22
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值