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. creating an array of existing WinForm controls

creating an array of existing WinForm controls

Scheduled Pinned Locked Moved C#
data-structuresquestion
5 Posts 4 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.
  • R Offline
    R Offline
    Roman Nurik
    wrote on last edited by
    #1

    how can i create an array of controls (well, pointers to existing controls that i made on my windows form) i want to be able to iterate through a collection of existing controls and modify their properties

    r -€

    R M H 3 Replies Last reply
    0
    • R Roman Nurik

      how can i create an array of controls (well, pointers to existing controls that i made on my windows form) i want to be able to iterate through a collection of existing controls and modify their properties

      r -€

      R Offline
      R Offline
      Roman Nurik
      wrote on last edited by
      #2

      as a side note, can i get an example of how to manage a pointer to a control like i want to have a pointer named pSelectedControl or something which will point to some text box or something but i get an error something about unsafe blocks

      r -€

      M 1 Reply Last reply
      0
      • R Roman Nurik

        how can i create an array of controls (well, pointers to existing controls that i made on my windows form) i want to be able to iterate through a collection of existing controls and modify their properties

        r -€

        M Offline
        M Offline
        Mike Ellison
        wrote on last edited by
        #3

        Hi Roman. Well, can you just iterate over the Controls collection for your form?

        1 Reply Last reply
        0
        • R Roman Nurik

          as a side note, can i get an example of how to manage a pointer to a control like i want to have a pointer named pSelectedControl or something which will point to some text box or something but i get an error something about unsafe blocks

          r -€

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          So, you're a former C++ developer then? ;-) First up, there's no need to create pointers as controls are reference types: a declaration

          Control ctl;

          creates a reference to a control, not an actual control - it acts like a C++ pointer, in that you can change which actual object it points to, but you can't actually find out the address. Any class (created using the class keyword) in C# is a reference type. The pointer syntax in C# is for when you want to do some direct bit manipulation of the object - when you want to perform unsafe casting or pointer arithmetic. C# forces you to say, using an unsafe block, when you want to do this - and you also have to pass the /unsafe switch to the compiler. Code compiled /unsafe can't be verified and must run with Full Trust - typically this means it must be copied to the local machine and run from there (this applies to the full Framework - the current version of the Compact Framework does not do security verification in this way). Stability. What an interesting concept. -- Chris Maunder

          1 Reply Last reply
          0
          • R Roman Nurik

            how can i create an array of controls (well, pointers to existing controls that i made on my windows form) i want to be able to iterate through a collection of existing controls and modify their properties

            r -€

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            You can either enumerate (or iterate) over the Controls collection property which already contains your controls that are displayed on a form, or add specific control references (as Mike mentioned, not "pointers"). If you only want to deal with controls of a specific type, for instance, then you could do something like this:

            foreach (Control c in Controls)
            {
            if (c is Button)
            {
            Button b = (Button)c;
            b.Text = "Click me!";
            }
            }

            When you enumerate a collection, list, or any other IEnumerable implementation, do not change the underlying enumerable otherwise an exception will be thrown. If you must change the collection or list or whatever, then iterate (the ol' for loop) over the collection or list instead and update your current index accordingly. There have been times when I wanted to keep a separate list or array (which is a static list, BTW) of certain controls in my form so I could deal with them in a loop as well. You could easily do something like this:

            Control[] controls = new Control[] {
            this.textBox1,
            this.textBox2,
            thix.button5
            };
            // Later...
            foreach (Control c in controls)
            // ...
            // -- OR --
            for (int i=0; i<controls.Length; i++)
            // ...

            Microsoft MVP, Visual C# My Articles

            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