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. what textboxes are on my form?

what textboxes are on my form?

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

    i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.

    M M D L 5 Replies Last reply
    0
    • G gonad

      i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      You can get controls in your form by this.Controls property in your form,then check the type of each one and if it is TextBox put it in an array. Mazy No sig. available now.

      1 Reply Last reply
      0
      • G gonad

        i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.

        M Offline
        M Offline
        mcgahanfl
        wrote on last edited by
        #3

        Let me give you a few clues. I am new at this so bear with me. 1. Read through all controls on the form, like this .. private ArrayList m_VisibleList = new ArrayList(); //poor name, it is all controls not just visible ones ... public void BuildListOfAllControlsOnForm() {//build a list of all controls on the form m_VisibleList.Clear(); m_VisibleList.Add(this); ArrayList aPendingControlListTemp = new ArrayList();//used to avoid aPendingControlListTemp.Add(this); System.Windows.Forms.Control CtrlParent = null; System.Windows.Forms.Control CtrlChild = null; for(int i=0; i < aPendingControlListTemp.Count; i++) {//progress thru for loop for the form and any tab pages or frame CtrlParent = (System.Windows.Forms.Control)aPendingControlListTemp[i]; for(int iIter = 0; iIter < CtrlParent.Controls.Count; iIter++) { CtrlChild = CtrlParent.Controls[iIter]; Debug.WriteLine("Parent hash " + CtrlParent.GetHashCode() + " Child hash " + CtrlChild.GetHashCode()); m_VisibleList.Add(CtrlChild); if(CtrlChild.Controls.Count > 0) {//tab page or frame control aPendingControlListTemp.Add(CtrlChild); } } } aPendingControlListTemp.Clear(); int iCount = 0; foreach(System.Windows.Forms.Control Ctrl in m_VisibleList) { Debug.WriteLine(++iCount + " " + Ctrl.Text ); } }//BuildListOfAllControlsOnForm 2nd. Loop through your list and ask which are Textboxes, that code would be something like this ... System.Windows.Forms.Control Traverse; where Traverse would point to an element in VisibleList. ... if(Traverse.GetType() == typeof(CheckBox) || Traverse.GetType().IsSubclassOf(typeof(CheckBox))) {//do special case handling for check box System.Windows.Forms.CheckBox pCheck = (System.Windows.Forms.CheckBox)Traverse; if(pCheck.Checked== false) { Traverse.Focus(); break; } } Let me know if this is too much and I will try to refine it for you.

        1 Reply Last reply
        0
        • G gonad

          i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          static void GetTextBoxes(Control parent, ArrayList list)
          {
          foreach (Control ctrl in parent.Controls)
          {
          if (ctrl as TextBox != null)
          list.Add(ctrl.Name);
          GetTextBoxes(ctrl, list);
          }
          }

          leppie::AllocCPArticle("Zee blog");
          Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

          1 Reply Last reply
          0
          • G gonad

            i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.

            D Offline
            D Offline
            David Stone
            wrote on last edited by
            #5

            This should work:

            ArrayList names = new ArrayList();
            foreach(Control ctrl in myForm.Controls)
            {
            if(ctrl is TextBox) names.Add(ctrl.Name);
            }


            I don't need no steenkin' LSD. If I want to see technicolour smears, I just give my daughter pizza for dinner. -Jamie Hale

            1 Reply Last reply
            0
            • G gonad

              i need to have all the names of the textboxes on my form put into an array. is there a way to dynamically set up an array of all the textboxes on my form? Thanks, .gonad.

              M Offline
              M Offline
              mcgahanfl
              wrote on last edited by
              #6

              All of the solutions given to you will work, if you just want textbox controls on the "FORM". But if your form contains tab-controls or frame-controls and you would like to know what/if these containers also contain textbox controls, then I think you need the solution I gave you. Lastly, if you do not care about textbox controls nested inside tab pages that are not active you could use GetNextControl(). Here is an example. public void SetFocusOnFirstEmptyControl() { int iCount=0; System.Windows.Forms.Control Traverse = this; //System.Reflection while(Traverse != null) { Debug.WriteLine(++iCount + " " + Traverse.Text ); //myChild.GetType().BaseType.Equals(typeof(myFatherClass)) if(Traverse.GetType() == typeof(CheckBox) || Traverse.GetType().IsSubclassOf(typeof(CheckBox))) {//do special case handling for check box System.Windows.Forms.CheckBox pCheck = (System.Windows.Forms.CheckBox)Traverse; if(pCheck.Checked== false) { Traverse.Focus(); break; } } if(Traverse.Text.Length == 0) {//test controls like textbox and dropdown Traverse.Focus(); break; } Traverse = GetNextControl(Traverse, true); }//while }//SetFocusOnFirstEmptyControl You have a lot of good people helping you. An answer is assured.

              G 1 Reply Last reply
              0
              • M mcgahanfl

                All of the solutions given to you will work, if you just want textbox controls on the "FORM". But if your form contains tab-controls or frame-controls and you would like to know what/if these containers also contain textbox controls, then I think you need the solution I gave you. Lastly, if you do not care about textbox controls nested inside tab pages that are not active you could use GetNextControl(). Here is an example. public void SetFocusOnFirstEmptyControl() { int iCount=0; System.Windows.Forms.Control Traverse = this; //System.Reflection while(Traverse != null) { Debug.WriteLine(++iCount + " " + Traverse.Text ); //myChild.GetType().BaseType.Equals(typeof(myFatherClass)) if(Traverse.GetType() == typeof(CheckBox) || Traverse.GetType().IsSubclassOf(typeof(CheckBox))) {//do special case handling for check box System.Windows.Forms.CheckBox pCheck = (System.Windows.Forms.CheckBox)Traverse; if(pCheck.Checked== false) { Traverse.Focus(); break; } } if(Traverse.Text.Length == 0) {//test controls like textbox and dropdown Traverse.Focus(); break; } Traverse = GetNextControl(Traverse, true); }//while }//SetFocusOnFirstEmptyControl You have a lot of good people helping you. An answer is assured.

                G Offline
                G Offline
                gonad
                wrote on last edited by
                #7

                :) you're right, i really appreciate your help on this and i have learned a LOT of stuff from everyone helping and i thank you. I got exactly what i needed and didn't even think about what you said regarding the textboxes on panels and tabs. i just thought it would show everything, but i was wrong. Thanks very much for everyone's help. :) .gonad.

                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