Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Iterating through Controls on A Form

Iterating through Controls on A Form

Scheduled Pinned Locked Moved C#
csharpwinformsquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AJ123
    wrote on last edited by
    #1

    Hi There.... I am looking to iterate through A form's Controls collection. However i am also interested in control which are the child of panels. Is there any built support in WinForms? Or is the solution just to write my own little recursive method? Thanks in advance Aj

    R 1 Reply Last reply
    0
    • A AJ123

      Hi There.... I am looking to iterate through A form's Controls collection. However i am also interested in control which are the child of panels. Is there any built support in WinForms? Or is the solution just to write my own little recursive method? Thanks in advance Aj

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Hi, yes recursion is the way to go:

      private void LoopThroughControls(Control c) {
      foreach (Control subControl in c) {
      //do something with c

        LoopThroughControls(c);
      

      }
      }

      Within your Form just call: LoopThroughControls(this); Robert

      A L 2 Replies Last reply
      0
      • R Robert Rohde

        Hi, yes recursion is the way to go:

        private void LoopThroughControls(Control c) {
        foreach (Control subControl in c) {
        //do something with c

          LoopThroughControls(c);
        

        }
        }

        Within your Form just call: LoopThroughControls(this); Robert

        A Offline
        A Offline
        AJ123
        wrote on last edited by
        #3

        Thanks Robert - Quite an elegant little solution there....:)

        1 Reply Last reply
        0
        • R Robert Rohde

          Hi, yes recursion is the way to go:

          private void LoopThroughControls(Control c) {
          foreach (Control subControl in c) {
          //do something with c

            LoopThroughControls(c);
          

          }
          }

          Within your Form just call: LoopThroughControls(this); Robert

          L Offline
          L Offline
          lainoo
          wrote on last edited by
          #4

          private void button3_Click(object sender, EventArgs e) { treeView1.Nodes.Add(LoopThroughControls(this)); } private TreeNode LoopThroughControls(Control c) { TreeNode node = new TreeNode(c.Name); Control ctemp; for(int i=0;iOk ...

          R 1 Reply Last reply
          0
          • L lainoo

            private void button3_Click(object sender, EventArgs e) { treeView1.Nodes.Add(LoopThroughControls(this)); } private TreeNode LoopThroughControls(Control c) { TreeNode node = new TreeNode(c.Name); Control ctemp; for(int i=0;iOk ...

            R Offline
            R Offline
            Robert Rohde
            wrote on last edited by
            #5

            What exactly are you trying to tell me with this? Robert

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups