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. Cycling through all GUI Objects within a form

Cycling through all GUI Objects within a form

Scheduled Pinned Locked Moved C#
csharptutorialquestion
24 Posts 11 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.
  • K karoitay

    Hi, Given a form I need to be able to collect the Name Property of all the GUI Objects within it. - Not Only Nested Controls, But also for example tabPages, ToolStripMenuItems and such. How can this be done? Thanks. (.NET 2.0)

    U Offline
    U Offline
    Urs Enzler
    wrote on last edited by
    #5

    by looping through all children of the MainForm and their children and so on. You can use either recursion or a Stack algorithm. To get the children use the Controls property of a Control. To speed things up use property HasChildren to check if a Control has childrens.

    -^-^-^-^-^- no risk no funk ................... please vote ------>

    1 Reply Last reply
    0
    • C Colin Angus Mackay

      Pete O`Hanlon wrote:

      Each object contains the Controls property

      Brilliant minds... :-D


      Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #6

      Colin Angus Mackay wrote:

      Brilliant minds...

      Sometimes our level of genius is quite embarrassing.:-D

      Deja View - the feeling that you've seen this post before.

      K 1 Reply Last reply
      0
      • P Pete OHanlon

        Each object contains the Controls property, which contain all of the controls that they constrain, so this might be a place to start. Hint - this can be done recursively.

        Deja View - the feeling that you've seen this post before.

        U Offline
        U Offline
        Urs Enzler
        wrote on last edited by
        #7

        Do not forget to check the HasChildren property. Speeds things up.

        -^-^-^-^-^- no risk no funk ................... please vote ------>

        P 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Iterate over the Controls collection.


          Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

          K Offline
          K Offline
          karoitay
          wrote on last edited by
          #8

          As I said I need not cycle over the controls only. For Ex: Assuming ther is a Control Within the Form of Type - ToolStrip The ToolStrip Class has no Controls Property Its Childeren are Beeing held in a collection which can be accessed with the Items Propery - Now - to get to them I cannot do what u have all mentioned above. So... my Question Remains - how can this be done?

          A 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Pete O`Hanlon wrote:

            Each object contains the Controls property

            Brilliant minds... :-D


            Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

            K Offline
            K Offline
            karoitay
            wrote on last edited by
            #9

            As I said I need not cycle over the controls only. For Ex: Assuming ther is a Control Within the Form of Type - ToolStrip The ToolStrip Class has no Controls Property Its Childeren are Beeing held in a collection which can be accessed with the Items Propery - Now - to get to them I cannot do what u have all mentioned above. So... my Question Remains - how can this be done?

            C 1 Reply Last reply
            0
            • P Pete OHanlon

              Colin Angus Mackay wrote:

              Brilliant minds...

              Sometimes our level of genius is quite embarrassing.:-D

              Deja View - the feeling that you've seen this post before.

              K Offline
              K Offline
              karthick n mca
              wrote on last edited by
              #10

              poda....................:laugh:

              1 Reply Last reply
              0
              • K karoitay

                As I said I need not cycle over the controls only. For Ex: Assuming ther is a Control Within the Form of Type - ToolStrip The ToolStrip Class has no Controls Property Its Childeren are Beeing held in a collection which can be accessed with the Items Propery - Now - to get to them I cannot do what u have all mentioned above. So... my Question Remains - how can this be done?

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #11

                Then you will have to use reflection to determine if you need to recurse into a Controls collection or an Items collection.


                Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                K M U 3 Replies Last reply
                0
                • K karoitay

                  As I said I need not cycle over the controls only. For Ex: Assuming ther is a Control Within the Form of Type - ToolStrip The ToolStrip Class has no Controls Property Its Childeren are Beeing held in a collection which can be accessed with the Items Propery - Now - to get to them I cannot do what u have all mentioned above. So... my Question Remains - how can this be done?

                  A Offline
                  A Offline
                  Anthony Mushrow
                  wrote on last edited by
                  #12

                  Iterate like people have said, but check the type of the object, and run a separate peice of code to get its items collection, or its child controls etc. Whatever you do, im pretty sure there isn't some simple function you can use, its gonna take quite the chunk of code, and your probably gonna have to write it all yourself.

                  K 1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    Then you will have to use reflection to determine if you need to recurse into a Controls collection or an Items collection.


                    Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                    K Offline
                    K Offline
                    karoitay
                    wrote on last edited by
                    #13

                    I Have just recently heard about reflection and I'm quite not familiar with the subject. So... can you please hand me a little piece of code to show how can this be done so I will not have to read all about reflection. ( I'm a litle busy the next days any I need this job to be done as quickly as possible ). Thanks.

                    J C 2 Replies Last reply
                    0
                    • C Colin Angus Mackay

                      Then you will have to use reflection to determine if you need to recurse into a Controls collection or an Items collection.


                      Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

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

                      Don't you think, that this would bring a lot of useless matches (ListBox.Items). I think it would be easier to check out how many control classes don't match "Controls", and do a typecheck ("as") at them!

                      All the best, Martin

                      C 1 Reply Last reply
                      0
                      • A Anthony Mushrow

                        Iterate like people have said, but check the type of the object, and run a separate peice of code to get its items collection, or its child controls etc. Whatever you do, im pretty sure there isn't some simple function you can use, its gonna take quite the chunk of code, and your probably gonna have to write it all yourself.

                        K Offline
                        K Offline
                        karthick n mca
                        wrote on last edited by
                        #15

                        you go to hell.............

                        D 1 Reply Last reply
                        0
                        • M Martin 0

                          Don't you think, that this would bring a lot of useless matches (ListBox.Items). I think it would be easier to check out how many control classes don't match "Controls", and do a typecheck ("as") at them!

                          All the best, Martin

                          C Offline
                          C Offline
                          Colin Angus Mackay
                          wrote on last edited by
                          #16

                          Martin# wrote:

                          Don't you think, that this would bring a lot of useless matches (ListBox.Items).

                          Since I don't know what the overall purpose is I can't say if that would bring lots of useless matches or not. It might do. It might not.


                          Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                          K 1 Reply Last reply
                          0
                          • C Colin Angus Mackay

                            Then you will have to use reflection to determine if you need to recurse into a Controls collection or an Items collection.


                            Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                            U Offline
                            U Offline
                            Urs Enzler
                            wrote on last edited by
                            #17

                            I wouldn't use reflection. I'd just test if the current control is of a type with a property Controls or whether it is of for example type ToolStrip: Or the make the code more readable, introduce several overloaded methods that take the different types as a parameter (one overload for control, one for toolstrip, ....) And call them recursively.

                            -^-^-^-^-^- no risk no funk ................... please vote ------>

                            1 Reply Last reply
                            0
                            • U Urs Enzler

                              Do not forget to check the HasChildren property. Speeds things up.

                              -^-^-^-^-^- no risk no funk ................... please vote ------>

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

                              That may depend on how the enumeration is implemented.

                              1 Reply Last reply
                              0
                              • K karthick n mca

                                you go to hell.............

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

                                Whould you mind explaining yourself?? Undefeated's explanation and suggestion is perfectly valid.

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

                                D 1 Reply Last reply
                                0
                                • C Colin Angus Mackay

                                  Martin# wrote:

                                  Don't you think, that this would bring a lot of useless matches (ListBox.Items).

                                  Since I don't know what the overall purpose is I can't say if that would bring lots of useless matches or not. It might do. It might not.


                                  Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                                  K Offline
                                  K Offline
                                  karoitay
                                  wrote on last edited by
                                  #20

                                  The Overall purpose is to have a dictionary that will map a Gui object to a bitmask to determine its accessibility to each user of the application

                                  1 Reply Last reply
                                  0
                                  • D Dave Kreskowiak

                                    Whould you mind explaining yourself?? Undefeated's explanation and suggestion is perfectly valid.

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

                                    D Offline
                                    D Offline
                                    Dan Neely
                                    wrote on last edited by
                                    #21

                                    but it wasn't something that could be copy/pasted as is to do the OPs homework/job for him. Consequently it was worthless. :mad:

                                    -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

                                    1 Reply Last reply
                                    0
                                    • K karoitay

                                      I Have just recently heard about reflection and I'm quite not familiar with the subject. So... can you please hand me a little piece of code to show how can this be done so I will not have to read all about reflection. ( I'm a litle busy the next days any I need this job to be done as quickly as possible ). Thanks.

                                      J Offline
                                      J Offline
                                      J4amieC
                                      wrote on last edited by
                                      #22

                                      karoitay wrote:

                                      I'm a litle busy the next days any I need this job to be done as quickly as possible

                                      Gee, why didn't you say so. I'll get right on it. :rolleyes:

                                      --- How to get answers to your questions[^]

                                      1 Reply Last reply
                                      0
                                      • K karoitay

                                        I Have just recently heard about reflection and I'm quite not familiar with the subject. So... can you please hand me a little piece of code to show how can this be done so I will not have to read all about reflection. ( I'm a litle busy the next days any I need this job to be done as quickly as possible ). Thanks.

                                        C Offline
                                        C Offline
                                        Colin Angus Mackay
                                        wrote on last edited by
                                        #23

                                        You can ask every object for its type with .GetType(). You get back a Type object. It has methods on it such as GetMethod, GetProperty and so on. You can get information from the objects those methods return MethodInfo or PropertyInfo and so on. If you find the one you want you can then Invoke it. Most of this should make enough sense through intellisense. You may have to look up some stuff in the documentation though. It really is worth reading up on so you have a better understanding of the environment in which you are working.


                                        Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                                        K 1 Reply Last reply
                                        0
                                        • C Colin Angus Mackay

                                          You can ask every object for its type with .GetType(). You get back a Type object. It has methods on it such as GetMethod, GetProperty and so on. You can get information from the objects those methods return MethodInfo or PropertyInfo and so on. If you find the one you want you can then Invoke it. Most of this should make enough sense through intellisense. You may have to look up some stuff in the documentation though. It really is worth reading up on so you have a better understanding of the environment in which you are working.


                                          Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                                          K Offline
                                          K Offline
                                          karoitay
                                          wrote on last edited by
                                          #24

                                          Thanks, I'll check it out.

                                          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