WHAT'S NEW?
Loading...

How to clear textbox in visual basic.net


Visual Basic.Net has a capability in clearing all textboxes and combo boxes in just one click. The usual way in clearing a textboxes using .Clear() keyword that clear only one textbox at a time or simply put “” represent null to clear textbox. However, in combo box, clear is not applicable to clear the text instead combobox.text =” “. For instance, if you have a lot of textbox in your form, it is difficult to manage which textbox you are going to clear, and it waste a lot of time typing the name of every textboxes. To address this problem, we are going to control all textboxes in order to clear in just one click.


Here are the codes:

Note: Just copy the code after End Sub, because you create new method that clear all textbox.

#Region "Clear all Textboxes"
    Public Sub ClearTextBox(ByVal ClearAll As Control)
        For Each ctrl As Control In ClearAll.Controls
            ClearTextBox(ctrl)
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Text = String.Empty
            End If
        Next ctrl
    End Sub
#End Region

After that, Copy ClearTextBox(Me) and paste in your button clear.

The same in ComboBox.
#Region "Clear all combobox"
    Public Sub clearcombo(ByVal ok As Control)
        For Each clr As Control In ok.Controls
            clearcombo(clr)
            If TypeOf clr Is ComboBox Then
                CType(clr, ComboBox).Text = String.Empty
            End If
        Next clr
    End Sub
#End Region

0 comments:

Post a Comment