Clear textboxes

prav_roy

Well-known member
Joined
Sep 10, 2005
Messages
70
Location
Mumbai
Programming Experience
1-3
hi,

i want to clear all the textboxes placed in a form using fallowing code, all text boxes are placed on PANEL control.. i am using following code.. it is not working can anybody tel me where i am going wrong

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
ClearTextBoxes(Me)
End Sub
Public Sub ClearTextBoxes(ByVal frm As Form)
Dim obj As New Control()
Dim obj1 As New Control()
Dim obj3 As New Control()
For Each obj In Me.Controls
If TypeOf obj Is TextBox Then
obj.Text = ""
End If
If TypeOf obj Is Panel Then
For Each obj1 In obj.Controls
If TypeOf obj1 Is TextBox Then
obj3.Text = ""
End If
Next
End If
Next
End Sub
 
try this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private S[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
     cleanUpForAWhile([/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2]
[/SIZE][COLOR=blue][SIZE=2]Private [/SIZE][SIZE=2]Sub[/SIZE][/COLOR][SIZE=2] cleanUpForAWhile([/SIZE][SIZE=2])
[/SIZE][SIZE=2] [COLOR=blue]Dim[/COLOR][/SIZE][SIZE=2] xControl [/SIZE][SIZE=2][COLOR=blue]As[/COLOR][/SIZE][SIZE=2] Control
[/SIZE][SIZE=2] [COLOR=blue]For [/COLOR][/SIZE][SIZE=2][COLOR=blue]Each[/COLOR][/SIZE][SIZE=2] xControl [/SIZE][SIZE=2][COLOR=blue]In[/COLOR] [/SIZE][SIZE=2][COLOR=blue]Me[/COLOR][/SIZE][SIZE=2].Panel1.Controls
[/SIZE][SIZE=2]      [COLOR=blue]If[/COLOR][/SIZE][SIZE=2] [COLOR=blue]xControl.GetType [/COLOR][/SIZE][COLOR=blue][SIZE=2]Is [/SIZE][SIZE=2]GetType[/SIZE][/COLOR][SIZE=2](TextBox) [/SIZE][SIZE=2][COLOR=blue]Then[/COLOR]
[/SIZE][SIZE=2]         xControl.Text = [/SIZE][SIZE=2][COLOR=blue]String[/COLOR][/SIZE][SIZE=2].Empty
[/SIZE][SIZE=2]      [COLOR=blue]End [/COLOR][/SIZE][SIZE=2][COLOR=blue]If[/COLOR]
[/SIZE][SIZE=2][COLOR=blue] Next[/COLOR]
[/SIZE][COLOR=blue][SIZE=2]End [/SIZE][SIZE=2]Sub[/SIZE][/COLOR]

Regards ;)
 
Or following your idea:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] cleanUpForAWhile([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] myForm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form)
[/SIZE][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] xControl [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control
[/SIZE][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] xControlPanel [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control
[/SIZE][SIZE=2][COLOR=#0000ff]   For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] xControlPanel [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] myForm.Controls
[/SIZE][SIZE=2][COLOR=#0000ff]      If[/COLOR][/SIZE][SIZE=2] xControlPanel.GetType [/SIZE][SIZE=2][COLOR=#0000ff]Is [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](Panel) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]         For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] xControl [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] xControlPanel.Controls
               xControl.Text = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty
[/SIZE][SIZE=2][COLOR=#0000ff]         Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]      End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
VB.NET:
For each Ctrl as control in Panel.controls
If Typeof ctrl is textbox then
ctrl.text = string.empty
end if
next

The above code assumes that they are all standard textboxes. If you have a mixture of textboxes and rich textboxes then you'll have to test for textbox base.

VB.NET:
If Type Of Ctrl Is TextboxBase then
 
Last edited:
Oh yeah Roy ... my both examples work flawless ... maybe you are doing something wrong there. Take a look of this attached sample and you'll see that it works ;)
vis it is recommended by MS that getting type is much better if you do that in the way i presented above. Well, at least it is that i've read somewhere in documentation.
 

Attachments

  • CleanUpTextBoxControls.zip
    28.6 KB · Views: 45
sorry to bother u again,
control does not go inside the statement

If xControl.GetType Is GetType(TextBox) Then
xControl.Text = String.Empty
End If

it goes to
If xControlPanel.GetType Is GetType(Panel) Then



 
Back
Top