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. each control in form

each control in form

Scheduled Pinned Locked Moved C#
helpquestion
9 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.
  • F Offline
    F Offline
    FernandoMartin
    wrote on last edited by
    #1

    hello, i have 5 checkboxes in the form, i want to do something like this: foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } } the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lot

    D N D M M 5 Replies Last reply
    0
    • F FernandoMartin

      hello, i have 5 checkboxes in the form, i want to do something like this: foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } } the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lot

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Controls can have child controls of their own. If you know the checkboxes are in a group or on a panel, all you have to do is look in the Controls collection of that container control. Also, don't EVER convert the type name to a string and compare it to what you think the full type name should be. You'll just end up writing code where you can't fathom what wrong with it.

      foreach (control c in Panel1.Controls)
          if (c is CheckBox)
             ...
      

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      1 Reply Last reply
      0
      • F FernandoMartin

        hello, i have 5 checkboxes in the form, i want to do something like this: foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } } the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lot

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        foreach (control c in Panel.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } }


        only two letters away from being an asset

        1 Reply Last reply
        0
        • F FernandoMartin

          hello, i have 5 checkboxes in the form, i want to do something like this: foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } } the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lot

          D Offline
          D Offline
          Dave Herren
          wrote on last edited by
          #4

          check the type of c and if it is a panel use the c.Controls to itenerate through it's children. -- modified at 16:21 Monday 21st May, 2007

          topcoderjax

          D 1 Reply Last reply
          0
          • D Dave Herren

            check the type of c and if it is a panel use the c.Controls to itenerate through it's children. -- modified at 16:21 Monday 21st May, 2007

            topcoderjax

            D Offline
            D Offline
            DoomedOne
            wrote on last edited by
            #5

            Recurse the controls as a tree. Call seekCheck pasing the form as first control. private void seekCheck (Control B) { foreach(Control Con in B.Controls) { if(Con.Controls != null) //is a container { seekCheck (Con); } else { if(Con id CheckBox) { MessageBox ("....."); } } } }

            1 Reply Last reply
            0
            • F FernandoMartin

              hello, i have 5 checkboxes in the form, i want to do something like this: foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } } the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lot

              M Offline
              M Offline
              Mundo Cani
              wrote on last edited by
              #6

              Fernando Jaspion wrote:

              if (c.GetType.ToString() == "System.Forms.CheckBox")

              You don't want to do a string compare here. What you want is: if (c.GetType() == typeof(System.Forms.CheckBox);

              Ian

              L 1 Reply Last reply
              0
              • F FernandoMartin

                hello, i have 5 checkboxes in the form, i want to do something like this: foreach (control c in this.Cotrols) { if (c.GetType.ToString() == "System.Forms.CheckBox") { MessageBox.Show("anything"); } } the problem is: if the checkboxes are inside a panel, ou a groupbox, i cant verify if exists checkboxes in the form... what i can do to find the checboxes that are inside de panel/groupbox? thanks a lot

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #7

                Hello, I mostely prefere to cast direct to the class I'm checking on, cause often a member of this class is on interest.

                CheckBox cb = c as CheckBox;
                if(cb!=null)

                A recursive method would check the "Controls" property of the Control instance c.

                RecursiveMethod(this.Controls);

                private void RecursiveMethod(ControlsCollection cc)
                {
                if(cc!=null)
                {
                foreach(Control c in cc)
                {
                CheckBox cb = c as CheckBox;
                if(cb!=null)
                {
                //Do stuff
                }
                else
                {
                RecursiveMethod(c.Controls);
                }
                }
                }
                }

                Hope it helps! All the best, Martin

                1 Reply Last reply
                0
                • M Mundo Cani

                  Fernando Jaspion wrote:

                  if (c.GetType.ToString() == "System.Forms.CheckBox")

                  You don't want to do a string compare here. What you want is: if (c.GetType() == typeof(System.Forms.CheckBox);

                  Ian

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  no no, just if (c is CheckBox) ... :)

                  Luc Pattyn [My Articles] [Forum Guidelines]

                  M 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    no no, just if (c is CheckBox) ... :)

                    Luc Pattyn [My Articles] [Forum Guidelines]

                    M Offline
                    M Offline
                    Mundo Cani
                    wrote on last edited by
                    #9

                    right you are! :-O

                    Ian

                    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