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. Is this an acceptable way to make a Control Array? [modified]

Is this an acceptable way to make a Control Array? [modified]

Scheduled Pinned Locked Moved C#
data-structuresquestion
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.
  • R Offline
    R Offline
    Randal Vance Cunanan
    wrote on last edited by
    #1

    Control[] controlArrays = new Control[3];

    controlArray[0] = textbox1; //name of the textbox control on the form
    controlArray[1] = textbox2;
    controlArray[2] = textbox3;

    for(int i = 0; i < controlArrays.Length; i++)
    {
    if(controlArray[i] is TextBox)
    (TextBox)controlArray[i].Text = "My Array #" + i.ToString();
    }

    Thanks.

    modified on Friday, December 4, 2009 12:13 AM

    A D G P L 7 Replies Last reply
    0
    • R Randal Vance Cunanan

      Control[] controlArrays = new Control[3];

      controlArray[0] = textbox1; //name of the textbox control on the form
      controlArray[1] = textbox2;
      controlArray[2] = textbox3;

      for(int i = 0; i < controlArrays.Length; i++)
      {
      if(controlArray[i] is TextBox)
      (TextBox)controlArray[i].Text = "My Array #" + i.ToString();
      }

      Thanks.

      modified on Friday, December 4, 2009 12:13 AM

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      You could create an array of type System.Windows.Forms.Control instead of object. After all, if you are only going to keep textboxes inside your array it does not make sense to use an object type. You could even use System.Windows.Forms.TextBoxBase if you really just need to store text boxes. Since you know your array elements anyway, you could also do something like - Control[] controlArrays = new Control[]{textbox1,textbox2,textbox3}; Looks neater, thats all ;) . I'm no expert, but I dont see any problems in storing controls inside an array.

      R 1 Reply Last reply
      0
      • A Abhinav S

        You could create an array of type System.Windows.Forms.Control instead of object. After all, if you are only going to keep textboxes inside your array it does not make sense to use an object type. You could even use System.Windows.Forms.TextBoxBase if you really just need to store text boxes. Since you know your array elements anyway, you could also do something like - Control[] controlArrays = new Control[]{textbox1,textbox2,textbox3}; Looks neater, thats all ;) . I'm no expert, but I dont see any problems in storing controls inside an array.

        R Offline
        R Offline
        Randal Vance Cunanan
        wrote on last edited by
        #3

        I got an error there, i really meant Control, not object. Thanks. I asked because I saw an article regarding making Control Arrays in C# but its longer and a lot more complicated.

        1 Reply Last reply
        0
        • R Randal Vance Cunanan

          Control[] controlArrays = new Control[3];

          controlArray[0] = textbox1; //name of the textbox control on the form
          controlArray[1] = textbox2;
          controlArray[2] = textbox3;

          for(int i = 0; i < controlArrays.Length; i++)
          {
          if(controlArray[i] is TextBox)
          (TextBox)controlArray[i].Text = "My Array #" + i.ToString();
          }

          Thanks.

          modified on Friday, December 4, 2009 12:13 AM

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

          If you are going to put only textboxes in the array, create an array of TextBoxes. That will avoid all that casting.

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

          1 Reply Last reply
          0
          • R Randal Vance Cunanan

            Control[] controlArrays = new Control[3];

            controlArray[0] = textbox1; //name of the textbox control on the form
            controlArray[1] = textbox2;
            controlArray[2] = textbox3;

            for(int i = 0; i < controlArrays.Length; i++)
            {
            if(controlArray[i] is TextBox)
            (TextBox)controlArray[i].Text = "My Array #" + i.ToString();
            }

            Thanks.

            modified on Friday, December 4, 2009 12:13 AM

            G Offline
            G Offline
            GWBas1c
            wrote on last edited by
            #5

            Consider using a List instead of an array, unless you have specific needs that dictate using an Array. The List class will automatically re-size as you add objects, yet you can still iterate and reference by index. For example:

            List controls = new List();

            controls.Add(textbox1);
            controls.Add(textbox2);
            controls.Add(textbox3);

            // ect...

            controls.Add(textbox99999999999);

            for(int i = 0; i < controlArrays.Length; i++)
            {
            if(controls[i] is TextBox)
            (TextBox)controls[i].Text = "Element #" + i.ToString();
            }

            1 Reply Last reply
            0
            • R Randal Vance Cunanan

              Control[] controlArrays = new Control[3];

              controlArray[0] = textbox1; //name of the textbox control on the form
              controlArray[1] = textbox2;
              controlArray[2] = textbox3;

              for(int i = 0; i < controlArrays.Length; i++)
              {
              if(controlArray[i] is TextBox)
              (TextBox)controlArray[i].Text = "My Array #" + i.ToString();
              }

              Thanks.

              modified on Friday, December 4, 2009 12:13 AM

              P Offline
              P Offline
              puri keemti
              wrote on last edited by
              #6

              Use Arraylist and then filter the Textbox controls from the form controls to get collection of controls.

              visit the link for more info : http://www.dreamincode.net/forums/showtopic45666.htm[^]

              modified on Friday, December 4, 2009 5:39 AM

              1 Reply Last reply
              0
              • R Randal Vance Cunanan

                Control[] controlArrays = new Control[3];

                controlArray[0] = textbox1; //name of the textbox control on the form
                controlArray[1] = textbox2;
                controlArray[2] = textbox3;

                for(int i = 0; i < controlArrays.Length; i++)
                {
                if(controlArray[i] is TextBox)
                (TextBox)controlArray[i].Text = "My Array #" + i.ToString();
                }

                Thanks.

                modified on Friday, December 4, 2009 12:13 AM

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Use a Generic List. It is much faster, resizes automatically and also avoids runtime type casts.

                List<TextBox> textBoxes = new List<TextBox>():
                textBoxes.Add(textBox1);
                textBoxes.Add(textBox2);
                textBoxes.Add(textBox2);

                int i =0;

                foreach(TextBox textBox in textBoxes) {
                textBox.Text = string.Format("My Array {0}", ++i);
                }

                1 Reply Last reply
                0
                • R Randal Vance Cunanan

                  Control[] controlArrays = new Control[3];

                  controlArray[0] = textbox1; //name of the textbox control on the form
                  controlArray[1] = textbox2;
                  controlArray[2] = textbox3;

                  for(int i = 0; i < controlArrays.Length; i++)
                  {
                  if(controlArray[i] is TextBox)
                  (TextBox)controlArray[i].Text = "My Array #" + i.ToString();
                  }

                  Thanks.

                  modified on Friday, December 4, 2009 12:13 AM

                  F Offline
                  F Offline
                  freakyit
                  wrote on last edited by
                  #8

                  Hi, take a look at the System.ComponentModel.CollectionBase class. you can write your own ControlCollection and do various operations on Adding to collection.. greetz

                  1 Reply Last reply
                  0
                  • R Randal Vance Cunanan

                    Control[] controlArrays = new Control[3];

                    controlArray[0] = textbox1; //name of the textbox control on the form
                    controlArray[1] = textbox2;
                    controlArray[2] = textbox3;

                    for(int i = 0; i < controlArrays.Length; i++)
                    {
                    if(controlArray[i] is TextBox)
                    (TextBox)controlArray[i].Text = "My Array #" + i.ToString();
                    }

                    Thanks.

                    modified on Friday, December 4, 2009 12:13 AM

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

                    That looks OK, but why would you want to?

                    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