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
|