您的位置:首页 > 其它

机房重构——清空与判空

2017-07-28 10:52 211 查看
【前言】

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

【正文】

如果只有几个地方需要清空,直接写下面的代码就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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: