【机房重构】--判空/清空方法(模块)

来源:互联网 发布:js 动态修改iconfont 编辑:程序博客网 时间:2024/06/04 19:17
     U层的每一个界面上都会用到判断文本框是否为空,和清除文本框内容的功能,所以我们应该会想到把这些功能抽象成方法然后供大家调用。 但是U层的这些公共方法放在那里呢,

     我想到了咱们第一次敲机房里面的模块,模块里面就存了很多公共方法,当然VB.Net里面也有存放公共方法的模块,下面跟大家分享一下如何实现吧:

1.新建一个CHeckMOdule模块。



Module CheckModule   '在模块中定义结构体Term,并定义term类型的结构体数组 Public Structure Term        Dim controlSub As Control        Dim strText As String        Sub New(ByVal controlSub As Control, ByVal strText As String)            With Me                .controlSub = controlSub                .strText = strText            End With        End Sub    End Structure    '定义一个term类型的结构体数组    Public arrayControl() As Term    ''' <summary>    ''' 判断数组中控件的text属性是否为空,并给出相应的提示    ''' </summary>    ''' <param name="arrayControl">需要便利的结构体数组</param>    ''' <returns>返回布尔值,true表示为空,false表示不为空</returns>    ''' <remarks></remarks>    Public Function CheckIsEmpty(ByVal arrayControl() As Term) As Boolean        Dim termControl As Term        '遍历结构体数组中所有的元素,如果控件文本为空,则进行相应提示        For Each termControl In arrayControl                                                              '遍历结构体数组中所有元素            If TypeOf termControl.controlSub Is TextBox Then                                     '判断控件是否为文本框                If termControl.controlSub.Text.Trim = "" Then                                        '判断文本框是否为空                    MessageBox.Show(termControl.strText & "不能为空哦!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)                    termControl.controlSub.Focus()                                                          '为空控件得到焦点                    Return True                    Exit Function                End If            ElseIf TypeOf termControl.controlSub Is ComboBox Then            '判断控件是否为组合框            End If            If termControl.controlSub.Text.Trim = "" Then                                   '判断组合框内容是否为空                MessageBox.Show(termControl.strText & "不能为空哦!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)                termControl.controlSub.Focus()                                                       '为空控件得到焦点                Return True                Exit Function            End If        Next        Return False    End Function    ''' <summary>    ''' 清空所有文本框,组合框的text    ''' </summary>    ''' <param name="arrayControl"></param>    ''' <returns></returns>    ''' <remarks></remarks>    Public Function AllEmpty(ByVal arrayControl() As Term) As Boolean        Dim termControl As Term        '遍历结构体数组中所有的元素,清空其text属性        For Each termControl In arrayControl                                                       '遍历结构体数组中所有元素            If TypeOf termControl.controlSub Is TextBox Or TypeOf termControl.controlSub Is ComboBox Then                termControl.controlSub.Text = ""            End If        Next        Return True    End FunctionEnd Module


以上是MOdule里面的内容。

下面我们以添加用户窗体为例子,展示如何引用:


  
 '在需要的窗体下定义一个过程Rdim(),作用是初始化这个trem类型的结构体数组    Private Sub Rdim()        ReDim Preserve arrayControl(4)  '重定义数组维数        '初始化数组        arrayControl(0) = New Term(txtUserID, "用户名")        arrayControl(1) = New Term(cboUserLevel, "用户级别")        arrayControl(2) = New Term(txtUserName, "姓名")        arrayControl(3) = New Term(txtPassword, "密码")        arrayControl(4) = New Term(txtDePassword, "确认密码")    End Sub     '写在具体事件里,调用模块方法'调用模块中的判空方法Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click        Call Rdim()        If CheckIsEmpty(arrayControl) Then            Exit Sub        End IfEnd Sub'调用模块中的清空方法Private Sub btnEmpty_Click(sender As Object, e As EventArgs) Handles btnEmpty.Click      Call Rdim()      If AllEmpty(arrayControl) Then          Exit Sub      End If  End Sub  

0 0