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. Get Controls

Get Controls

Scheduled Pinned Locked Moved C#
tutorialquestion
16 Posts 8 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.
  • P Pedram Behroozi

    Hi All I have two panel in my form, my 1st panel contains 3 buttons and the 2nd has 2 buttons, 3 textboxes and a label. I need to get all of these controls to save their settings into a file, I tried:

    foreach (Control ctrl in this.Controls)
    MyFile.ReadLine(ctrl.Text); // For example

    But "this" has only two controls (My two panels) and I can't see the other controls contained in panels. I know I can use the foreach statement for each of my panels but if I have a panel in panel2? Do you have any idea to solve this using only one loop? Regards.

    While (true) { Human.isLearnable = true; }

    Z Offline
    Z Offline
    zafersavas
    wrote on last edited by
    #3

    You can use something like this:

    foreach (Form frm in Application.OpenForms)
    {
    foreach (Control ctrl in frm.Controls)
    MessageBox.Show(ctrl.Text);
    }

    This contains two foreach statement but indepedent of how many forms you have, you can get their controls. zafer

    P 1 Reply Last reply
    0
    • Z zafersavas

      You can use something like this:

      foreach (Form frm in Application.OpenForms)
      {
      foreach (Control ctrl in frm.Controls)
      MessageBox.Show(ctrl.Text);
      }

      This contains two foreach statement but indepedent of how many forms you have, you can get their controls. zafer

      P Offline
      P Offline
      Pedram Behroozi
      wrote on last edited by
      #4

      You're right, but I have only one form, my form contains two Panels and each Panels contains some Controls. Your foreach statements give me my Panels only. Thanks

      While (true) { Human.isLearnable = true; }

      1 Reply Last reply
      0
      • P PIEBALDconsult

        Each Panel has a Controls property, no?

        P Offline
        P Offline
        Pedram Behroozi
        wrote on last edited by
        #5

        Yes it has but I don't want to have a foreach statement per panel. Thanks

        While (true) { Human.isLearnable = true; }

        realJSOPR 1 Reply Last reply
        0
        • P Pedram Behroozi

          Hi All I have two panel in my form, my 1st panel contains 3 buttons and the 2nd has 2 buttons, 3 textboxes and a label. I need to get all of these controls to save their settings into a file, I tried:

          foreach (Control ctrl in this.Controls)
          MyFile.ReadLine(ctrl.Text); // For example

          But "this" has only two controls (My two panels) and I can't see the other controls contained in panels. I know I can use the foreach statement for each of my panels but if I have a panel in panel2? Do you have any idea to solve this using only one loop? Regards.

          While (true) { Human.isLearnable = true; }

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #6

          Use two foreach statements. Like this: foreach (Control oCtrl in this.Controls){ // Do something foreach(Control oCtrl1 in oCtrl.Controls){ // Do something } }

          "If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry

          P 1 Reply Last reply
          0
          • D dan sh

            Use two foreach statements. Like this: foreach (Control oCtrl in this.Controls){ // Do something foreach(Control oCtrl1 in oCtrl.Controls){ // Do something } }

            "If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry

            P Offline
            P Offline
            Pedram Behroozi
            wrote on last edited by
            #7

            It's possible only for two panels. I wont be glad if i have 3 panels in my form and each one contains 4 panels inside... X| however thanks. :)

            While (true) { Human.isLearnable = true; }

            D 1 Reply Last reply
            0
            • P Pedram Behroozi

              It's possible only for two panels. I wont be glad if i have 3 panels in my form and each one contains 4 panels inside... X| however thanks. :)

              While (true) { Human.isLearnable = true; }

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #8

              In that case you can use conditional recursion by checking if current control has child controls.

              "If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry

              P 1 Reply Last reply
              0
              • D dan sh

                In that case you can use conditional recursion by checking if current control has child controls.

                "If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #9

                Yeah, that's how I'd do it.

                realJSOPR 1 Reply Last reply
                0
                • P Pedram Behroozi

                  Hi All I have two panel in my form, my 1st panel contains 3 buttons and the 2nd has 2 buttons, 3 textboxes and a label. I need to get all of these controls to save their settings into a file, I tried:

                  foreach (Control ctrl in this.Controls)
                  MyFile.ReadLine(ctrl.Text); // For example

                  But "this" has only two controls (My two panels) and I can't see the other controls contained in panels. I know I can use the foreach statement for each of my panels but if I have a panel in panel2? Do you have any idea to solve this using only one loop? Regards.

                  While (true) { Human.isLearnable = true; }

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #10

                  Each 'container' has it's own control collection so you have to loop each one of them. The most easiest way is to create a method for listing controls and call this method recursively passing the collection (for example List) as a parameter. Pseudo-code would be something like:

                  GetControls(List controlList, Control parent) {
                  foreach (Control ctrl in parent.Controls) {
                  controlList.Add(ctrl); //or whatever you want to do
                  GetControls(controlList, ctrl);
                  }
                  }

                  The need to optimize rises from a bad design

                  1 Reply Last reply
                  0
                  • P Pedram Behroozi

                    Yes it has but I don't want to have a foreach statement per panel. Thanks

                    While (true) { Human.isLearnable = true; }

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #11

                    Well then, just find something else to do with your time. If it's not freakin' obvious what you have to do, and that "not wanting to" isn't a viable tack if you want to accomplish your goal, you shouldn't be a programmer. I bet you could find a job sweeping standing water off of sidewalks. Oh wait, that requires a little effort as well. Oh wait! Try just staring off into space. That should keep your feeble excuse for a brain busy.

                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Yeah, that's how I'd do it.

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #12

                      You guys are wasting your time. He probably thinks "recursion" is having to visit the toilet more than once in a 60-second time frame.

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      1 Reply Last reply
                      0
                      • P Pedram Behroozi

                        Hi All I have two panel in my form, my 1st panel contains 3 buttons and the 2nd has 2 buttons, 3 textboxes and a label. I need to get all of these controls to save their settings into a file, I tried:

                        foreach (Control ctrl in this.Controls)
                        MyFile.ReadLine(ctrl.Text); // For example

                        But "this" has only two controls (My two panels) and I can't see the other controls contained in panels. I know I can use the foreach statement for each of my panels but if I have a panel in panel2? Do you have any idea to solve this using only one loop? Regards.

                        While (true) { Human.isLearnable = true; }

                        G Offline
                        G Offline
                        Giorgi Dalakishvili
                        wrote on last edited by
                        #13

                        Ever heard about Recursion ?[^]

                        Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

                        G 1 Reply Last reply
                        0
                        • G Giorgi Dalakishvili

                          Ever heard about Recursion ?[^]

                          Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #14

                          Or recursion[^]? ;)

                          G 1 Reply Last reply
                          0
                          • P Pedram Behroozi

                            Hi All I have two panel in my form, my 1st panel contains 3 buttons and the 2nd has 2 buttons, 3 textboxes and a label. I need to get all of these controls to save their settings into a file, I tried:

                            foreach (Control ctrl in this.Controls)
                            MyFile.ReadLine(ctrl.Text); // For example

                            But "this" has only two controls (My two panels) and I can't see the other controls contained in panels. I know I can use the foreach statement for each of my panels but if I have a panel in panel2? Do you have any idea to solve this using only one loop? Regards.

                            While (true) { Human.isLearnable = true; }

                            P Offline
                            P Offline
                            Pedram Behroozi
                            wrote on last edited by
                            #15

                            Sorry if I'm wasting your time... I'm not an expert like you... Yeah, I know Recursion, I'll use it... Thanx :)

                            While (true) { Human.isLearnable = true; }

                            1 Reply Last reply
                            0
                            • G Guffa

                              Or recursion[^]? ;)

                              G Offline
                              G Offline
                              Giorgi Dalakishvili
                              wrote on last edited by
                              #16

                              An unhandled exception of type 'System.StackOverflowException' occurred in me. :)

                              Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

                              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