Trouble adding control to control collection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Suikwan
    New Member
    • Feb 2007
    • 11

    Trouble adding control to control collection

    Im trying to add the textboxes in a form to a control collection which I will test later. For some reason my control collection ends up empty. None of the controls are added. Any help would be greatly appreciated. Code follows.

    Code:
    Dim contcoll As ControlCollection
    Dim cont As Control
    
    contcoll = New ControlCollection(Me)
    
    For Each cont In Me.Controls
         If TypeOf cont Is TextBox Then
              contcoll.Add(cont)
         End If
    Next
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If your controls are in a Container control (such as a groupbox) they will not show up as being in Me.Controls.
    You would have to examine all the controls in Me.Controls[] for the .HasChildren and if so, search through them(and their children and so on)

    Comment

    • Suikwan
      New Member
      • Feb 2007
      • 11

      #3
      Originally posted by Plater
      If your controls are in a Container control (such as a groupbox) they will not show up as being in Me.Controls.
      You would have to examine all the controls in Me.Controls[] for the .HasChildren and if so, search through them(and their children and so on)
      The controls were added directly to the fom and are not in any container.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Have you tried seing whats in there?

        With say this (I might not have the foreach declration correct)
        [code=vbnet]
        For Each Control c In Me.Controls
        MessageBox.Show (c.GetType().Na me);
        Next
        [/code]

        Comment

        • Suikwan
          New Member
          • Feb 2007
          • 11

          #5
          Originally posted by Plater
          Have you tried seing whats in there?

          With say this (I might not have the foreach declration correct)
          [code=vbnet]
          For Each Control c In Me.Controls
          MessageBox.Show (c.GetType().Na me);
          Next
          [/code]
          Yes, I did it by outputting the name of each control to a text file and the list was correct.

          Code:
          For i = 0 To (me.controls.Count - 1)
                 File.AppendAllText("c:\testingemailallcontrols.txt", me.controls.Item(i).Name & vbCrLf)
                  'Next

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Hmm, well I don't know why your collection is empty.

            Do you need to make it be:
            Code:
            For Each cont In Me.Controls.Items
            ?

            Don't you need to compare the types of either side?
            Code:
            If TypeOf cont Is TypeOf TextBox Then
            Did you then try to add EVERY control from Me.Controls into your collection?

            Comment

            • Suikwan
              New Member
              • Feb 2007
              • 11

              #7
              Originally posted by Plater
              Hmm, well I don't know why your collection is empty.

              Do you need to make it be:
              Code:
              For Each cont In Me.Controls.Items
              ?

              Don't you need to compare the types of either side?
              Code:
              If TypeOf cont Is TypeOf TextBox Then
              Did you then try to add EVERY control from Me.Controls into your collection?
              The original syntax is correct. I tried adding all the controls to the control collection and then removing the ones that are not textboxes but not all the non-textbox controls are removed. The two buttons are removed but only six of the nine labels are removed.

              Code:
              Dim contcoll As ControlCollection = Me.Controls
              Dim cont As Control
              
              For Each cont In contcoll
                 If Not TypeOf cont Is TextBox Then
              	   contcoll.Remove(cont)
                 End If
              Next
              By the way, how do you get it to recognize that the text posted is vb code?

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                I think there is something else wrong here, I will investigate reproducing it.




                EDIT: AHHA!!

                It's because of the nature of ControlCollecti on! If you look at the description on the .Add() you will see that it says "Adds a control to the form". Which explains why the contructor requires a Form argument.
                All you're doing is adding the control back to the same form.

                Try this:
                [code=vbnet]
                Dim contcoll As New List(Of Control)()
                For Each cont As Control In Me.Controls
                If TypeOf cont Is TextBoxThen
                contcoll.Add(co nt)

                End If
                Next
                [/code]

                You can use:
                [code=vbnet] instead of [code]

                Comment

                • Suikwan
                  New Member
                  • Feb 2007
                  • 11

                  #9
                  Originally posted by Plater
                  I think there is something else wrong here, I will investigate reproducing it.




                  EDIT: AHHA!!

                  It's because of the nature of ControlCollecti on! If you look at the description on the .Add() you will see that it says "Adds a control to the form". Which explains why the contructor requires a Form argument.
                  All you're doing is adding the control back to the same form.

                  Try this:
                  [code=vbnet]
                  Dim contcoll As New List(Of Control)()
                  For Each cont As Control In Me.Controls
                  If TypeOf cont Is TextBoxThen
                  contcoll.Add(co nt)

                  End If
                  Next
                  [/code]

                  You can use:
                  [code=vbnet] instead of [code]
                  SWEEEEEEEEEEEEE T!!!!

                  Thanks for the help. Hopefully the bruises on my head will heal soon :)

                  Comment

                  Working...