VB.NET 完美解决判断文本框、组合框为空问题

前端之家收集整理的这篇文章主要介绍了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.  
  11. For Each control In frm.Controls '遍历窗体中所有的控件
  12. If TypeOf control Is TextBox Then '判断控件是不是文本框
  13. If control.Text.Trim = "" Then '判断文本框内容是否为空
  14. MsgBox(control.Tag.ToString + "不能为空!",vbOKOnly,"温馨提示")
  15. control.Focus()
  16. Return True
  17. Exit Function
  18. End If
  19. ElseIf TypeOf control Is ComboBox Then '判断控件是不是组合框
  20. If control.Text.Trim = "" Then
  21. MsgBox(control.Tag.ToString + "不能为空!","温馨提示")
  22. Return True
  23. Exit Function
  24. End If
  25. End If
  26. Next
  27.  
  28. Return False
  29. 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.  
  10. For Each control In arrayControl '遍历数组中所有元素
  11. If TypeOf control Is TextBox Then '判断控件是不是文本框
  12. If control.Text.Trim = "" Then '判断文本框内容是否为空
  13. MsgBox(control.Tag.ToString + "不能为空!","温馨提示")
  14. Return True
  15. Exit Function
  16. End If
  17. End If
  18. Next
  19. Return False
  20. End Function

调用函数

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

猜你在找的VB相关文章