机房收费--模块应用:控件为空、全部清空、是否为数字

一时失言乱红尘 2022-08-07 01:57 205阅读 0赞
  1. 机房收费进行有一段时日了,今天对注册窗体的大量文本框和下拉列表进行操作时引发思考:指定文本框判断是否为空、是否为数字、清空文本框的代码都是大同小异的,依据相同归一原则,是否能将它们放到一个类中?

我们知道在vb中用到传统的方法是:把文本框粘贴复制成为数组,通过循环的方式遍历每一个文本框判定是否为空;要使弹出框提示内容与文本框相对应(如,“txtCardID”为空,则提示“卡号不能为空”),可以通过定义转换函数的方法来实现。

  1. 它存在的缺点是:控件数组的命名将不能直观体现文本框含义,我们必须将数组元素与文本框的真实含义相对应,否则就会出错。例如:txtbox0)对应 “卡号”,如果错认为对应“学号“,提示内容将出现混乱。
  2. vb.net取消了控件数组的方法,我们再次粘贴复制的时候形成的是单个控件而不能形成数组。这时,我们必须另寻它法。通过查阅博客,找到了两个比较好的方法,与大家共勉:一个是王海涛师哥写的[《VB.NET 完美解决判断文本框、组合框为空问题》][VB.NET];一个是霍亚静师姐[《vb.net & 文本框为空提示,一键清空文本框所有内容》][vb.net _]。
  3. 师哥用的是封装类的方法,通过定义控件基类,将控件封装进数组遍历来判断是否为空:
  4. Public Shared Function IsAllEmptyText(ByVal frm As Form) As Boolean
  5. Dim control As New Control
  6. For Each control In frm.Controls '遍历窗体中所有的控件
  7. If TypeOf control Is TextBox Then '判断控件是不是文本框
  8. If control.Text.Trim = "" Then '判断文本框内容是否为空
  9. MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
  10. control.Focus()
  11. Return True
  12. Exit Function
  13. End If
  14. ElseIf TypeOf control Is ComboBox Then '判断控件是不是组合框
  15. If control.Text.Trim = "" Then
  16. MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
  17. Return True
  18. Exit Function
  19. End If
  20. End If
  21. Next
  22. Return False
  23. End Function
  24. 所不同的是师姐采用结构体的思想,将所要提示的文字信息和控件封装进结构体(在U层建立模块窗体):
  25. Public Structure term '定义结构体
  26. Dim controlSub As Control '控件类型基类来储存控件
  27. Dim strText As String '储存文字
  28. Sub New(ByVal controlSub As Control, ByVal strText As String) 'new方法来实例化控件
  29. With Me
  30. .controlSub = controlSub
  31. .strText = strText
  32. End With
  33. End Sub
  34. End Structure
  35. 说到 [结构体][Link 1],我是比较陌生的,可以将其理解为功能更为强大的数据类型。一个复杂的事物往往由一个变量来描述是不行的,需要许多不同类型的变量共同描述,就像“时钟”可以由“时、分、秒”共同描述一样;上例中的term结构体由控件control和字符string共同描述,可以用来存储多种类型的数据。
  36. 第二步,定义存储结构体的数组,用于存放所要判断的控件信息:
  37. Public arrayControl() As term '存放结构体的数组
  38. 第三步,定义要实现的函数,可以将对文本框的常用判断都封装成函数,这里封装了是否为空,全部清空和是否为数字的判断方法:
  39. Public arrayControl() As term '存放结构体的数组
  40. '判断是否为空函数
  41. Public Function CIsEmpty(ByVal ArrayControl() As term) As Boolean
  42. Dim termControl As New term '实例化数组元素
  43. For Each termControl In ArrayControl '每一个结构体数组中的元素
  44. '结构体的控件是文本框或者下拉列表框时,判断是否为空
  45. If (TypeOf termControl.controlSub Is TextBox) Or (TypeOf termControl.controlSub Is ComboBox) Then
  46. If termControl.controlSub.Text.Trim = "" Then '为空时提示不能为空,并获得焦点,返回true
  47. MessageBox.Show(termControl.strText & "不能为空!!!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  48. termControl.controlSub.Focus()
  49. Return True
  50. Exit Function
  51. End If
  52. End If
  53. Next
  54. Return False '返回false,表示没有为空的文本框
  55. End Function
  56. '将指定的文本框清空
  57. Public Function AllEmpty(ByVal Arraycontrol() As term) As Boolean
  58. Dim termControl As New term
  59. For Each termControl In Arraycontrol
  60. If TypeOf termControl.controlSub Is TextBox Then
  61. termControl.controlSub.Text = ""
  62. End If
  63. Next
  64. Return True
  65. End Function
  66. '判断指定的文本框是否为数字
  67. Public Function IsNum(ByVal Arraycontrol() As term) As Boolean
  68. Dim termControl As New term
  69. For Each termControl In Arraycontrol
  70. If TypeOf termControl.controlSub Is TextBox Then
  71. If IsNumeric(termControl.controlSub.Text.Trim) = False Then
  72. MessageBox.Show(termControl.strText & "必须为数字!!!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  73. termControl.controlSub.Text = ""
  74. termControl.controlSub.Focus()
  75. Return True
  76. Exit Function
  77. End If
  78. End If
  79. Next
  80. Return False
  81. End Function
  82. 实现方法:在U层用sub方法收集结构体数组,使用时直接用Call调用
  83. Private Sub Rdim()
  84. ReDim Preserve arrayControl(7)
  85. arrayControl(0) = New term(txtCardID, "卡号")
  86. arrayControl(1) = New term(txtStuID, "学号")
  87. arrayControl(2) = New term(txtName, "姓名")
  88. arrayControl(3) = New term(txtSeriers, "系别")
  89. arrayControl(4) = New term(txtGrade, "年级")
  90. arrayControl(5) = New term(txtClass, "班级")
  91. arrayControl(6) = New term(txtDescribe, "描述")
  92. End Sub
  93. Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
  94. '清空指定控件内容
  95. Call Rdim()
  96. If AllEmpty(arrayControl) Then
  97. Exit Sub
  98. End If
  99. End Sub
  100. 结构体建立和函数的封装都是建立在U层的模块当中,其实跟我们敲击vb版机房收费系统模块是相同的,里面存储一些U层显示内容使用的静态方法,对比类而言,无法实现继承,无法实现接口。详细内容推荐一篇博文《[VB.NET模块与众不同之处》][VB.NET 1]。
  101. 任何方法都要考虑他的利弊,它对于控件较为繁杂的窗体是非常适用的,但如果一个窗体中只有两个文本框就大可不必使用如此繁琐的方式了,盲目的使用只会增加计算机的负担。诸如判断文本框内容是否为数字,使用IsNumeric方法就可以搞定,只有当判断的文本框比较多时才考虑将控件封装到结构体数组中。另外combox控件其实是不必判断为空的,从全心全意为人民服务的角度出发,应该在窗体登陆时就在这些控件中填充上常用的数据。

总结:

  1. 任何时候都应该使用批判的眼光去吸收所运用的知识,只有这样才能慢慢摸索门道,一种方法什么时候该用,什么时候好用!

发表评论

表情:
评论列表 (有 0 条评论,205人围观)

还没有评论,来说两句吧...

相关阅读