您的位置:首页 > 其它

判断输入信息是否为空

2014-08-11 07:33 148 查看
在机房收费系统中,我们需要对文本框和组合框重复进行判断,确保不为空;该判断有两种情况,第一种,判断窗体中所有文本框组合框是否为空,第二种,判断一部分文本框,组合框是否为空。对于卡号和学号等我们需要判断用户输入的是否是数字,几乎每个窗体都需要进行相类似的判断,一个一个去写,熟悉了代码没错,可是,这个方法似乎不是那么聪明哈,这个时候,我们就可以定义一个类,专门用来进行判断,使用该功能的窗体直接调用类中的方法即可。接下来,简单介绍一下,该如何实现。

首先,判断窗体中所有文本框、组合框是否为空;

<span style="font-size:18px;">Imports System.Windows.Forms

'**********************************************
'文 件 名: verdict
'命名空间: UI
'内    容:
'功    能: 判断用户输入是否为空,判断输入的用户名等一系列是数字的文本框是否是数字
'文件关系:
'作    者:丁国华
'小    组:宝贝计划
'生成日期: 2014/8/5 10:32:09
'版本号:V2.0
'修改日志:
'版权说明:
'**********************************************

Public Class verdict
''' <summary>
''' 判断窗体中所有文本框、组合框输入内容是否为空,若窗体中有允许为空的文本框或组合框,
'''则不能使用此函数
''' </summary>
''' <param name="frm"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function IsAllEmptyText(ByVal frm As Form) As Boolean
Dim control As New Control
For Each control In frm.Controls '遍历窗体中所有的控件
If TypeOf control Is TextBox Then '判断控件是不是文本框
If control.Text.Trim = "" Then '判断文本框内容是否为空
MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
control.Focus()
Return True
Exit Function
End If
ElseIf TypeOf control Is ComboBox Then '判断控件是不是组合框
If control.Text.Trim = "" Then
MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
Return True
Exit Function
End If
End If
Next

Return False
End Function</span>
接着,判断一部分文本框、组合框是否为空;

<span style="font-size:18px;"> ''' <summary>
''' 判断控件数组中的控件的Text属性是否为空
''' </summary>
''' <param name="arrayControl"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function IsSomeEmptyText(ByVal arrayControl() As Control) As Boolean
Dim control As New Control

For Each control In arrayControl '遍历数组中所有元素
If TypeOf control Is TextBox Then '判断控件是不是文本框
If control.Text.Trim = "" Then '判断文本框内容是否为空
MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
control.Focus()
Return True
Exit Function
End If
ElseIf TypeOf control Is ComboBox Then '判断控件是不是组合框
If control.Text.Trim = "" Then
MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
Return True
Exit Function
End If
End If
Next
Return False
End Function</span>
最后,判断是否为数字;

<span style="font-size:18px;"> ''' <summary>
''' 判断输入的是否为数字
''' </summary>
''' <param name="arrayControl"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function IsNumberic(ByVal arrayControl() As Control) As Boolean
Dim control As New Control
For Each control In arrayControl '遍历数组中所有元素
If TypeOf control Is TextBox Then '判断控件是不是文本框
'If control.Text.Trim = "" Then '判断文本框内容是否为空
If IsNumeric(control.Text) = False Then
'MsgBox(control.Tag.ToString + "不能为空!", vbOKOnly, "温馨提示")
MsgBox(control.Tag.ToString + "   " + "请输入数字", vbOKOnly, "提示")
control.Focus()
control.Text = ""
Return False
Exit Function
End If
End If
Next
Return True
End Function</span>
紧接着,我们以机房收费系统中,基本数据设定为例,看看我们是如何进行调用的;

<span style="font-size:18px;">        Dim arrayControl() As Control
ReDim Preserve arrayControl(4)
arrayControl(0) = txtRate
arrayControl(1) = txtUnittime

arrayControl(2) = txtLeasttime
arrayControl(3) = txtPretime
arrayControl(4) = txtLimitcash
If verdict.IsSomeEmptyText(arrayControl) Then
Exit Sub
End If
If verdict.IsNumberic(arrayControl) = False Then
Exit Sub
End If</span>
把公共需要使用的部分,抽象出来写成一个类,其余的窗体直接进行调用,这样方便,简单,第二版机房收费系统,未完,待续......
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐