How to clear textboxes text  
Author Message
amit9979





PostPosted: Visual Basic Express Edition, How to clear textboxes text Top

Hi! Members

Pls tell how to clear all textboxes text in the same form within a single click on the button.

without using this sytax:

textbox1.clear

textbox2.clear

-

-

-

-

and so on.

in button click event

Thanks

Amit




Visual Studio Express Editions29  
 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, How to clear textboxes text Top

does this help



for each currentControl as Control in Me.Controls
if TypeOf currentControl is TextBox Then
Dim theTextBox as new TextBox
theTextBox = DirectCast(currentControl, TextBox)
theTextBox.Text = ""
end if
next



 
 
amit9979





PostPosted: Visual Basic Express Edition, How to clear textboxes text Top

Hi! ahmedilyas

Its work.

Thanks !!!!

Amit




 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, How to clear textboxes text Top

glad I could help!

 
 
spotty





PostPosted: Visual Basic Express Edition, How to clear textboxes text Top

You'll need to ensure that you handle textboxes which may be nested in containing controls such as panels, groupboxes, tabs etc.     The above code will not handle these nested textboxes in containing controls.

For this create a form with a panel, group box and put a textbox on the form and in each of these controls,  add text into each and then play around with the calls - you can clear all or clear only those in a specific containing control.

This should work.

Public Class Form1

    Public Sub Cleardata(ByVal container As Control)
        For Each c As Control In container.Controls
            If TypeOf c Is TextBox Then
                CType(c, TextBox).Clear()
            End If
            If c.Controls.Count > 0 Then
                Cleardata(c)
            End If
        Next
    End Sub

    Private Sub ClearOnlyPanel1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Cleardata(Me.Panel1)
    End Sub

    Private Sub ClearAll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Cleardata(Me)
    End Sub

End Class