Results 1 to 12 of 12

Thread: [RESOLVED] How to clear all textboxes

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    36

    Resolved [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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    36

    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

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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

  6. #6
    New Member
    Join Date
    Nov 2010
    Posts
    11

    Re: How to clear all textboxes

    Textbox1.Text = ""

    the end

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    36

    Re: How to clear all textboxes

    Quote Originally Posted by p h o e n i x View Post
    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?
    Last edited by NinjaZec; Nov 22nd, 2010 at 08:24 PM.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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.
    My usual boring signature: Nothing

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How to clear all textboxes

    This will guaranteely clear all textboxes on a form:

    vb Code:
    1. Public Sub ClearTextBoxes(Optional ByVal ctlcol As Control.ControlCollection = Nothing)
    2.         If ctlcol Is Nothing Then ctlcol = Me.Controls
    3.         For Each ctl As Control In ctlcol
    4.             If TypeOf (ctl) Is TextBox Then
    5.                 DirectCast(ctl, TextBox).Clear()
    6.             Else
    7.                 If Not ctl.Controls Is Nothing OrElse ctl.Controls.Count <> 0 Then
    8.                     ClearTextBoxes(ctl.Controls)
    9.                 End If
    10.             End If
    11.         Next
    12.     End Sub

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    36

    Re: How to clear all textboxes

    @Shaggy Hiker you are completely right they are part of ToolStripContainer1.ContentPanel
    What do i do now

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    36

    Re: How to clear all textboxes

    This works great, thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width