机房重构——清空与判空
2017-07-28 10:52
211 查看
【前言】
机房重构的时候会遇到很多清空,判空的地方,下面来总结一下清空判空的办法。
【正文】
如果只有几个地方需要清空,直接写下面的代码就OK了。
可是如果要清空很多的地方,上面那种方法就比较麻烦。
可以利用模块来进行清空或者判空
一般都是在U层中进行清空或者判空,所以模块就写在U层,
然后在需要清空的方法里面写上如下代码
机房重构的时候会遇到很多清空,判空的地方,下面来总结一下清空判空的办法。
【正文】
如果只有几个地方需要清空,直接写下面的代码就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
相关文章推荐
- 【机房重构】--判空/清空方法(模块)
- 机房重构---清空文本框或组合框内容
- 机房重构---修改密码
- 个人重构机房收费系统——类图
- 机房重构---透过数据库设计体验学习的快乐
- 机房重构七层登录之问题总结
- 机房重构DataGridView控件应用
- 个人机房重构——组合查询及优化
- 重构机房收费系统之 模板方法模式
- 【机房重构】——反射的机制
- 机房收费系统重构之SQLhelper的理解
- 机房收费系统重构(—)—小试牛刀
- C#机房重构——DataGridView控件
- 机房重构之开篇
- 重构机房收费系统(二)
- 【机房个人重构】sqlHelper------让我们的代码更高效
- 【机房重构】如何将Excel表格导出到WPS
- 机房重构报表显示方式
- 机房重构之泛型集合的应用
- 走在机房重构的路上之七层(二)