Results 1 to 6 of 6

Thread: [RESOLVED] For each control next loop

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Resolved [RESOLVED] For each control next loop

    hi all,

    i was trying to find a way to clear multiple textboxes.text in my form.

    i found out about this code and tried it but having problems tracing textboxes only.

    Code:
        Private Sub Clear_Fields(ByVal CForm As Form)
            Dim CControl As System.Windows.Forms.Control
            Dim tb As System.Windows.Forms.TextBox
            For Each CControl In CForm.Controls
                CControl.Text = ""
            Next
        End Sub
    unfortunately this code erases all text not only textboxes so i tried this

    Code:
        Private Sub Clear_Fields(ByVal CForm As Form)
            Dim CControl As System.Windows.Forms.Control
            Dim tb As System.Windows.Forms.TextBox
            For Each CControl In CForm.Controls
                If CControl.GetType Is tb.GetType Then
                CControl.Text = ""
                End If
            Next
        End Sub
    this doesn't do anything unfortunately...

    so then i tried this

    Code:
        Private Sub Clear_Fields(ByVal CForm As Form)
            Dim tb As System.Windows.Forms.TextBox
            For Each tb In CForm.Controls
                tb.Text = ""
            Next
        End Sub
    it didnt work as well so im out of ideas right now... any suggestions?
    thanks.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: For each control next loop

    Hi there,
    For all of the controls that are directly on your form:
    Code:
            For Each ctl As Control In Me.Controls
                If TypeOf ctl Is TextBox Then
                    ctl.Text = String.Empty
                End If
            Next ctl

  3. #3
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: For each control next loop

    Quote Originally Posted by adshocker
    hi all,

    i was trying to find a way to clear multiple textboxes.text in my form.

    i found out about this code and tried it but having problems tracing textboxes only.

    Code:
        Private Sub Clear_Fields(ByVal CForm As Form)
            Dim CControl As System.Windows.Forms.Control
            Dim tb As System.Windows.Forms.TextBox
            For Each CControl In CForm.Controls
                CControl.Text = ""
            Next
        End Sub
    unfortunately this code erases all text not only textboxes so i tried this

    Code:
        Private Sub Clear_Fields(ByVal CForm As Form)
            Dim CControl As System.Windows.Forms.Control
            Dim tb As System.Windows.Forms.TextBox
            For Each CControl In CForm.Controls
                If CControl.GetType Is tb.GetType Then
                CControl.Text = ""
                End If
            Next
        End Sub
    this doesn't do anything unfortunately...

    so then i tried this

    Code:
        Private Sub Clear_Fields(ByVal CForm As Form)
            Dim tb As System.Windows.Forms.TextBox
            For Each tb In CForm.Controls
                tb.Text = ""
            Next
        End Sub
    it didnt work as well so im out of ideas right now... any suggestions?
    thanks.
    Hi,

    You should do this:

    Code:
    For each Tb In Me.Controls
    If tb Is TextBox then
    tb.Text=""
    End If 
    Next
    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: For each control next loop

    thanks... both suggestions worked.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  5. #5
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: For each control next loop

    Glad to help.

    Note that those are only for TextBoxes right on your form. If you have TextBoxes in containers that you want to clear, you'll need to do it a little differently.

    Code:
            Dim ctl As Control = Me.GetNextControl(Me, True)
            Do Until ctl Is Nothing
                If TypeOf ctl Is TextBox Then
                    ctl.Text = String.Empty
                End If
    
                ctl = Me.GetNextControl(ctl, True)
            Loop
    And don't forget to mark the thread resolved.

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: For each control next loop

    Quote Originally Posted by adshocker
    thanks... both suggestions worked.
    Hi,

    Mark your thread as resolved then.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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