推断输入信息是否为空

       在机房收费系统中。我们须要对文本框和组合框反复进行推断,确保不为空;该推断有两种情况,第一种,推断窗口中全部文本框组合框是否为空,另外一种,推断一部分文本框,组合框是否为空。对于卡号和学号等我们须要推断用户输入的是否是数字。差点儿每一个窗口都须要进行相相似的推断。一个一个去写,熟悉了代码没错,但是,这种方法似乎不是那么聪明哈,这个时候,我们就能够定义一个类,专门用来进行推断,使用该功能的窗口直接调用类中的方法就可以。接下来,简介一下。该怎样实现。

       首先,推断窗口中全部文本框、组合框是否为空;

        

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

'**********************************************
'文 件 名: verdict
'命名空间: UI
'内    容:
'功    能: 推断用户输入是否为空。推断输入的username等一系列是数字的文本框是否是数字
'文件关系:
'作    者:丁国华
'小    组:宝贝计划
'生成日期: 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>
         把公共须要使用的部分,抽象出来写成一个类,其余的窗口直接进行调用。这样方便,简单。第二版机房收费系统。未完。待续......

 

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/zhchoutai/p/8516023.html