机房收费系统总结之4——VB.NET 轻松解决判断文本框、组合框为空问题

前端之家收集整理的这篇文章主要介绍了机房收费系统总结之4——VB.NET 轻松解决判断文本框、组合框为空问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

纵观机房收费系统,判断文本框、组合框为空问题无非两种情况。第一种:判断窗体中所有文本框、组合框是否为空。第二种:判断一部分文本框、组合框是否为空。下面看看是如何实现这两种情况的。


第一种:判断窗体中所有文本框、组合框是否为空。

  1. ''' <summary>
  2. ''' 判断窗体中所有文本框、组合框输入内容是否为空,若窗体中有允许为空的文本框或组合框,
  3. '''则不能使用此函数
  4. ''' </summary>
  5. ''' <param name="frm"></param>
  6. ''' <returns></returns>
  7. ''' <remarks></remarks>
  8. Public Shared Function IsAllEmptyText(ByVal frm As Form) As Boolean
  9. Dim control As New Control
  10. For Each control In frm.Controls '遍历窗体中所有的控件
  11. If TypeOf control Is TextBox Then '判断控件是不是文本框
  12. If control.Text.Trim = "" Then '判断文本框内容是否为空
  13. MsgBox(control.Tag.ToString + "不能为空!",vbOKOnly,"温馨提示")
  14. control.Focus()
  15. Return True
  16. Exit Function
  17. End If
  18. ElseIf TypeOf control Is ComboBox Then '判断控件是不是组合框
  19. If control.Text.Trim = "" Then
  20. MsgBox(control.Tag.ToString + "不能为空!","温馨提示")
  21. Return True
  22. Exit Function
  23. End If
  24. End If
  25. Next
  26. Return False
  27. End Function

第二种:判断一部分文本框、组合框是否为空。
  1. ''' <summary>
  2. ''' 判断控件数组中的控件的Text属性是否为空
  3. ''' </summary>
  4. ''' <param name="arrayControl"></param>
  5. ''' <returns></returns>
  6. ''' <remarks></remarks>
  7. Public Shared Function IsSomeEmptyText(ByVal arrayControl() As Control) As Boolean
  8. Dim control As New Control
  9. For Each control In arrayControl '遍历数组中所有元素
  10. If TypeOf control Is TextBox Then '判断控件是不是文本框
  11. If control.Text.Trim = "" Then '判断文本框内容是否为空
  12. MsgBox(control.Tag.ToString + "不能为空!","温馨提示")
  13. Return True
  14. Exit Function
  15. End If
  16. End If
  17. Next
  18. Return False
  19. End Function

调用函数
  1. Dim arrayControl() As Control
  2. ReDim Preserve arrayControl(1)
  3. arrayControl(0) = txtUserName
  4. arrayControl(1) = txtPassword
  5. If UIEmpty.IsSomeEmptyText(arrayControl) Then
  6. Exit Sub
  7. End If

按上面执行的话,发现存在些问题
1.此方法遍历时是从后向前弹出对话框。比如:用户名和密码都为空,它会先弹出密码不能为空,再弹出用户名不能为空。我们如果采用此方法,在放置控件时需注意一下顺序。
2.控件不能跨容器遍历。比如控件不在一个容器内,有在form体中的,也有在GroupBox中的,就得分别单独判断!



为此自己进行了一下改进,但相应的麻烦了一些。
自己定义了一个结构体数组,把控件和对应的文本框信息(如:学号、姓名……)进行封装,遍历到相应的控件,如果为空,就可以有对应的提示


1.定义一个结构体数组:

  1. '定义结构体Term
  2. Private Structure Term
  3. Dim controlSub As Control
  4. Dim strText As String
  5. Sub New(ByVal controlSub As Control,ByVal strText As String)
  6. With Me
  7. .controlSub = controlSub
  8. .strText = strText
  9. End With
  10. End Sub
  11. End Structure

2.定义一个该类型的结构体数组,并初始化:

  1. '定义一个term类型的结构体数组
  2. Dim arrayControl() As Term
  3. ReDim Preserve arrayControl(7)
  4. '初始化数组
  5. arrayControl(0) = New Term(txtCardNo,"卡号")
  6. arrayControl(1) = New Term(txtStudentNo,"学号")
  7. arrayControl(2) = New Term(txtName,"姓名")
  8. arrayControl(3) = New Term(comboSex,"性别")
  9. arrayControl(4) = New Term(txtDepName,"系别")
  10. arrayControl(5) = New Term(comboGrade,"年级")
  11. arrayControl(6) = New Term(txtClass,"班级")
  12. arrayControl(7) = New Term(txtMoney,"金额")

3.调用函数进行判断:

  1. '调用IsSomeEmptyText()函数,检测是否输入文本内容
  2. If IsSomeEmptyText(arrayControl) Then
  3. Exit Sub
  4. End If

函数内容

  1. ''' <summary>
  2. ''' 判断控件数组中的控件的Text属性是否为空,并进行相应提示
  3. ''' </summary>
  4. ''' <param name="arrayControl">需要遍历的结构体数组</param>
  5. ''' <returns>Boolean值,true表示为空,false表示不为空</returns>
  6. Private Function IsSomeEmptyText(ByVal arrayControl() As Term) As Boolean
  7. Dim termControl As Term '声明Term类型变量termControl
  8. '遍历结构体数组中所有元素,如结构体中的控件文本为空,则找其对就的字符串进行相应提示
  9. For Each termControl In arrayControl '遍历结构体数组中所有元素
  10. If TypeOf termControl.controlSub Is TextBox Then '判断控件是不是文本框
  11. If termControl.controlSub.Text.Trim = "" Then '判断文本框内容是否为空
  12. MessageBox.Show(termControl.strText & "不能为空","",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
  13. termControl.controlSub.Focus()
  14. Return True
  15. Exit Function
  16. End If
  17. ElseIf TypeOf termControl.controlSub Is ComboBox Then '判断控件是不是组合框
  18. If termControl.controlSub.Text.Trim = "" Then '判断文本框内容是否为空
  19. MessageBox.Show(termControl.strText & "不能为空",MessageBoxIcon.Exclamation)
  20. termControl.controlSub.Focus()
  21. Return True
  22. Exit Function
  23. End If
  24. End If
  25. Next
  26. Return False
  27. End Function


综上所述,所改进的方法,略显麻烦,但通过手动写入内容显示时还是蛮灵活的。

1.不受任何容器的限制。

2.控件项随意显示。不需要显示的,像只读文本框“充值余额”,就完全不用搭理他。

3.顺序随意。结构体数据初始化,给他个什么顺序,就会按要求乖乖显示

猜你在找的VB相关文章