VB.NET-輕鬆判斷文字框是否為空白,一鍵清空文字框內容

來源:互聯網
上載者:User

標籤:.net   物件導向   combobox   textbox   struct   

    房收費系統的時候,令人相當噁心的一件事就是判斷文字框和組合框是否為空白還有清空文字框。基本上每個表單都要進行判斷,那一個接著一個的If...Else...語句,長長一串,看著就頭疼,但是第一次做機房收費系統的時候竟然傻傻的一個不落都寫了出來。真佩服當時的自己,不過這一次還是不要在做那種傻事了,因為我們經曆了不少物件導向的洗礼,認識了封裝。

         向對象的範疇裡,對於相同的或者類似的代碼只要重複3次以上我們應該想到物件導向的三大特性之一:封裝。想是想到了但是具體怎麼通過封裝來簡化繁瑣的判斷任務呢?

1. 首先建立一個模組,在UI層右鍵-添加-建立項-選擇 Model

              

2. 然後在Model中定義一個結構體Term,且定義一個term類型的結構體數組

          構體(struct)是由一系列具有相同類型或不同類型的資料構成的資料集合,也叫結構。和基礎資料類型Int,Char一樣,可以把它定義成自己需要的資料類型。 詳情請百度一下。

<span style="font-size:18px;">    '定義結構體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</span>

3. 然後我們就可以在需要的表單下定義一個過程Rdim() 作用是初始化這個Trem類型的結構體數組。

    <span style="font-size:18px;">Private Sub Rdim()        ReDim Preserve arrayControl(9)    '重定義數組維數        '初始化數組        arrayControl(0) = New Term(txtCID, "卡號")        arrayControl(1) = New Term(txtBalance, "儲值金額")        arrayControl(2) = New Term(txtSID, "學號")        arrayControl(3) = New Term(txtName, "姓名")        arrayControl(4) = New Term(cmbsex, "性別")        arrayControl(5) = New Term(txtDepartment, "系別")        arrayControl(6) = New Term(cmbGrade, "年級")        arrayControl(7) = New Term(txtClass, "班級")        arrayControl(8) = New Term(cmbType, "類型")        arrayControl(9) = New Term(cmbstatus, "卡的狀態")    End Sub</span>


 

4. 然後該函數了,先說IsEmptyText()函數,作用是判斷文字框、組合框的內容是否為空白,為空白的話給出相應的提示。它同樣寫在Model中

    ''' <summary>      ''' 判斷數組中控制項的Text屬性是否為空白,並給出相應提示      ''' </summary>      ''' <param name="arrayControl">需要遍曆的結構體數組</param>      ''' <returns>返回Boolean值,true表示為空白,false表示不為空白</returns>      Public Function CheckIsEmpty(ByVal arrayControl() As Term) As Boolean        Dim termControl As Term                                     '聲明一個Term類型變數termControl        '遍曆結構體數組中的所有元素,如果控制項文本為空白,則進行相應提示          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   '判斷控制項是否為組合框                 If termControl.controlSub.Text.Trim = "" Then       '判斷組合框內容是否為空白                    MessageBox.Show(termControl.strText & "不可為空哦!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)                    termControl.controlSub.Focus()                  '為空白控制項得到焦點                    Return True                    Exit Function                End If            End If        Next        Return False    End Function

 

5. 一鍵清除文字框內容的函數,在Model中添加函數AllEmpty()

    ''' <summary>    ''' 清空表單中所有文字框、組合框的Text    ''' </summary>    ''' <param name="arrayControl">需要遍曆的結構體數組</param>    ''' <returns>返回布爾值,True</returns>    ''' <remarks></remarks>    Public Function AllEmpty(ByVal arrayControl() As Term) As Boolean        Dim termControl As Term                                     '聲明一個Term類型變數termControl        '遍曆結構體數組中的所有元素,清空其Text屬性        For Each termControl In arrayControl                        '遍曆結構體數組中所有元素            If TypeOf termControl.controlSub Is TextBox Then        '判斷控制項是否為文字框                If termControl.controlSub.Text.Trim <> "" Then    '判斷文字框內容是否為空白                    termControl.controlSub.Text = ""                End If            ElseIf TypeOf termControl.controlSub Is ComboBox Then   '判斷控制項是否為組合框                 If termControl.controlSub.Text.Trim <> "" Then    '判斷組合框內容是否為空白                    termControl.controlSub.Text = ""                End If            End If        Next        Return True    End Function

 

6. 上面說明了這麼多,現在到調用的時候了。

檢查是否為空白:

    Private Sub btnEmpty_Click(sender As Object, e As EventArgs) Handles btnEmpty.Click        Call Rdim()   ’首先調用Rdim過程進行數組初始化        '再調用IsEmptyText()函數,檢測是否輸入常值內容        If CheckIsEmpty(arrayControl) Then            Exit Sub        End If    End Sub


一鍵清空:

    Private Sub btnEmpty_Click(sender As Object, e As EventArgs) Handles btnEmpty.Click        Call Rdim()   ’首先調用Rdim過程進行數組初始化        '調用清空函數,把表單中控制項的文本清空        If AllEmpty(arrayControl) Then            Exit Sub        End If    End Sub

其他表單應用該函數的時候只需要定義一個過程Rdim() 初始化Trem類型的結構體數組,再調用函數即可。

非常方便!隨著學習,正在逐漸的觸摸到物件導向思想所帶來的好處。

 

(PS:不當的地方,懇請指出!)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.