[RESOLVED] How to clear all textboxes
I just started learning programing if you can call it that, i had a class on visual basic a while ago and i managed to create a nice little application now i want to implement a menu button which when you click will erase all data in all textboxes
So i managed to create a buton but although i have found many answers to my question on the net not one code had worked for me, even the one from this forums faq doesn't work.
If is probably coz i don't understand where to copy it or i am doing something wrong
I am using Windows Form Application
I tried putting the code like this but obviously it doesn't work
Code:
Private Sub ClearEverthingToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearEverthingToolStripMenuItem.Click
Dim ctl As Control
' Clear all the TextBoxes on the form.
For Each ctl In Controls
If TypeOf ctl Is TextBox Then ctl.Text = ""
Next ctl
End Sub
Re: How to clear all textboxes
There are several ways that this might not work. How about telling us what does happen? Do you get errors, or does it just do nothing. There are a couple obvious reasons why it might do nothing. For example, that code will only clear textboxes that are on the form itself rather than on panels on the form, or inside groupboxes, tab pages, and so forth.
So what is happening that should not happen, and what is not happening that should happen?
Re: How to clear all textboxes
nothing is happening, no errors
i just dropped all the textboxes in the form.
I want something that will erase all the data from all the textboxes that i have
Re: How to clear all textboxes
Are you sure you dropped these forms on the form, not on the panel, not on group box, not on somewhere else? Me.Controls will hold only the textboxes which are hosted on the form itself, i.e. if you have a panel on the form and a textbox on that panel, Me.Controls will only have a panel. The panel .Controls collection will have textboxes instead.
Re: How to clear all textboxes
Maybe did you c&p a control?
ClearTextBox(me)
Code:
'Clears all textboxes
Public Sub ClearTextBox(ByVal root As Control)
For Each ctrl As Control In root.Controls
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
End If
Next ctrl
End Sub
Re: How to clear all textboxes
Textbox1.Text = ""
the end
Re: How to clear all textboxes
Quote:
Originally Posted by
p h o e n i x
Textbox1.Text = ""
the end
except i have like 100 textboxes
i have no idea what is a panel i dropped them on the first thing that was there when i started a new project you tell me if its a panel or what it is :)
i started learning it yesterday so i still have no clue about almost anything in VS
@ident can you explain me where should i copy it?
Re: How to clear all textboxes
What ident posted is the same thing that you posted in the first post. The only difference is that one is in a method callable from other places, and the other one is your code. If it didn't work in the first version, it still won't work in the only vaguely different second version.
The problem is most likely that your textboxes are not actually part of the form. Take a look at the document Outline found under View|Other Windows, which will show what the controls are contained in. I would expect that you would find your textboxes are not part of the form, but part of some other container.
Re: How to clear all textboxes
This will guaranteely clear all textboxes on a form:
vb Code:
Public Sub ClearTextBoxes(Optional ByVal ctlcol As Control.ControlCollection = Nothing)
If ctlcol Is Nothing Then ctlcol = Me.Controls
For Each ctl As Control In ctlcol
If TypeOf (ctl) Is TextBox Then
DirectCast(ctl, TextBox).Clear()
Else
If Not ctl.Controls Is Nothing OrElse ctl.Controls.Count <> 0 Then
ClearTextBoxes(ctl.Controls)
End If
End If
Next
End Sub
Re: How to clear all textboxes
@Shaggy Hiker you are completely right they are part of ToolStripContainer1.ContentPanel
What do i do now :)
Re: How to clear all textboxes
Ok, if they are ALL part of the same control, that makes it easier. You could add the method that ident posted (it would just be another sub on the form, just like all the others), and pass in the ToolStripContainer1.ContentPanel that you just mentioned (I assume that is a control, though I haven't actually checked). Alternatively, you could take the code you posted in the first post:
Code:
For Each ctl In Controls
If TypeOf ctl Is TextBox Then ctl.Text = ""
Next ctl
And change it to:
Code:
For Each ctl In ToolStripContainer1.ContentPanel.Controls
If TypeOf ctl Is TextBox Then ctl.Text = ""
Next ctl
Or you could use the recursive solution that Cicatrix supplied, though I would recommend against that if all of the textboxes are in the same container, because it is overkill which you might later regret.
Re: How to clear all textboxes
This works great, thank you very much :)