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. Indexed textboxes

Indexed textboxes

Scheduled Pinned Locked Moved C#
databasedata-structurestutorialquestion
7 Posts 5 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 Offline
    P Offline
    patgo
    wrote on last edited by
    #1

    Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!

    S C U N 4 Replies Last reply
    0
    • P patgo

      Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Yes it is, but obviously you won't be able to use it from the designer. You can create them like

      class MyForm : Form
      {
      private TextBox []boxes;

      public MyForm(int count)
      {
      boxes = new TextBox[count];
      foreach(TextBox box in boxes)
      {
      // Set each box's locations.
      this.Controls.Add(box);
      }
      }
      }

      Regards
      Senthil
      _____________________________
      My Blog | My Articles | WinMacro

      P 1 Reply Last reply
      0
      • P patgo

        Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!

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

        Yes. If you are creating the text boxed dynamically then it is easy enough to put them in an array. If the text boxes are created in the designer then you need to populate an array after InitialiseComponent* is called - the array should be declared as a member variable in your form class. You can then refer to the array and iterate through it. * DISCLAIMER: This may not be the exact name of the method, I'm going from memory here. In any event I am referring to the method that Visual Studio modifies when you make changes in the designer.


        My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        P 1 Reply Last reply
        0
        • S S Senthil Kumar

          Yes it is, but obviously you won't be able to use it from the designer. You can create them like

          class MyForm : Form
          {
          private TextBox []boxes;

          public MyForm(int count)
          {
          boxes = new TextBox[count];
          foreach(TextBox box in boxes)
          {
          // Set each box's locations.
          this.Controls.Add(box);
          }
          }
          }

          Regards
          Senthil
          _____________________________
          My Blog | My Articles | WinMacro

          P Offline
          P Offline
          patgo
          wrote on last edited by
          #4

          Thank you very much!!!

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            Yes. If you are creating the text boxed dynamically then it is easy enough to put them in an array. If the text boxes are created in the designer then you need to populate an array after InitialiseComponent* is called - the array should be declared as a member variable in your form class. You can then refer to the array and iterate through it. * DISCLAIMER: This may not be the exact name of the method, I'm going from memory here. In any event I am referring to the method that Visual Studio modifies when you make changes in the designer.


            My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

            P Offline
            P Offline
            patgo
            wrote on last edited by
            #5

            Thank you very much!!!

            1 Reply Last reply
            0
            • P patgo

              Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!

              U Offline
              U Offline
              User 1180270
              wrote on last edited by
              #6

              An alternative approach could be to generate the array dynamically, the framework is already storing references to all of the controls, both static and dynamic, so you can just recurse through the ControlCollections pulling out all of the ones of the required type. private void searchControlTree(ControlCollection colControls , ArrayList allCtls, System.Type ctlType) { foreach (Control c in colControls ) { if ( c.GetType() == ctlType) allCtls.Add(c) ; if (c.Controls.Count > 0 ) searchControlTree(c.Controls, allCtls, ctlType ) ; } } public ArrayList findControlByType( System.Type ctlType) { ArrayList allCtls = new ArrayList(); searchControlTree(this.Controls, allCtls, ctlType) ; return allCtls ; } ArrayList a = findControlByType( typeof(TextBox)) ; If you need a real array at the end the ArrayList has a ToArray() method. If you combine this approach to finding the controls together with the control Name or Tag properties you can filter out whatever controls you want. Hope this is usefull Jackson

              1 Reply Last reply
              0
              • P patgo

                Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!

                N Offline
                N Offline
                nemopeti
                wrote on last edited by
                #7

                I think there is better way to use for or foreach for with textbox. Example make the textboxes inside a groupbox and then: //the groupbox called gb foreach(Control c in gb) { //if teh current controll is a textbox if(c.GetType().ToString()=="System.Windows.Forms.TextBox") { c.Text="Hello"; } } I often use this trick to clear all trextboxes in my form :->

                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