在机房重构中,经常重复遇到的事情就是判断输入框是否为空,并且清除输入框的内容。
当在系统中,遇到同样的代码出现三次及以上时,就要思考将他们封装起来,减少冗余。
所以,利用循环数组的方法,依次判断输入框是否为空,并一键清除输入框内容。
下面以注册用户为例,介绍一下,封装的过程:
首先,先要定义一个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>
<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 判断语句。在下面一篇文章中,继续进行封装。将介绍“正则表达式”,对输入框的输入字符条件,进行判断。