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. iterating controls on the form

iterating controls on the form

Scheduled Pinned Locked Moved C#
question
6 Posts 3 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.
  • M Offline
    M Offline
    mcgahanfl
    wrote on last edited by
    #1

    I realize I can get a list of controls on the form by writing this.Contols; Is there a simple way to get the controls collection back sorted by tab order? thanks

    D 1 Reply Last reply
    0
    • M mcgahanfl

      I realize I can get a list of controls on the form by writing this.Contols; Is there a simple way to get the controls collection back sorted by tab order? thanks

      D Offline
      D Offline
      Donald_a
      wrote on last edited by
      #2

      I don't think there is a way built into the framework to do this - would probably require the use of some kind of sortable collection EG SortedList or ArrayList...

      C 1 Reply Last reply
      0
      • D Donald_a

        I don't think there is a way built into the framework to do this - would probably require the use of some kind of sortable collection EG SortedList or ArrayList...

        C Offline
        C Offline
        Corinna John
        wrote on last edited by
        #3

        That seems to be a good start. List the Controls in the SortedList and write a custom comparer: //somewhere in the code SortedList myList = new SortedList(yourComparer, this.Controls.Count); foreach(Control ctl in this.Controls){ myList.Add(ctl); } //somewhere else class ControlComparer : IComparer { public bool Compare(object obj1, object obj2){ Control ctl1 = (Control)obj1; Control ctl2 = (Control)obj2; bool returnValue = false; //compare and set [returnValue] return returnValue; } }

        D M 3 Replies Last reply
        0
        • C Corinna John

          That seems to be a good start. List the Controls in the SortedList and write a custom comparer: //somewhere in the code SortedList myList = new SortedList(yourComparer, this.Controls.Count); foreach(Control ctl in this.Controls){ myList.Add(ctl); } //somewhere else class ControlComparer : IComparer { public bool Compare(object obj1, object obj2){ Control ctl1 = (Control)obj1; Control ctl2 = (Control)obj2; bool returnValue = false; //compare and set [returnValue] return returnValue; } }

          D Offline
          D Offline
          Donald_a
          wrote on last edited by
          #4

          I was thinking of something more like:

          //Sorted list to hold all controls
          SortedList ControlListByTabIndex = new SortedList();
          			
          //Loop over controls
          foreach(Control CurrentControl in this.Controls)
          {
              //Add to sortedlist, using the tabindex as the key, and the control itself as the value.
              ControlListByTabIndex.Add(CurrentControl.TabIndex, CurrentControl);
          }
          
          //You can now loop over the list, and it is ordered by TabIndex
          for (int i = 0 ; i < ControlListByTabIndex.Count ; i++)
            {
              Console.WriteLine(((Control)ControlListByTabIndex.GetByIndex(i)).TabIndex);
              Console.WriteLine(((Control)ControlListByTabIndex.GetByIndex(i)).Name);
          }
          
          1 Reply Last reply
          0
          • C Corinna John

            That seems to be a good start. List the Controls in the SortedList and write a custom comparer: //somewhere in the code SortedList myList = new SortedList(yourComparer, this.Controls.Count); foreach(Control ctl in this.Controls){ myList.Add(ctl); } //somewhere else class ControlComparer : IComparer { public bool Compare(object obj1, object obj2){ Control ctl1 = (Control)obj1; Control ctl2 = (Control)obj2; bool returnValue = false; //compare and set [returnValue] return returnValue; } }

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

            Thank you. It turns out that controls have a GetNextControl() property. This avoids the need to recursively iter throught tab controls and frame controls. thanks again.

            1 Reply Last reply
            0
            • C Corinna John

              That seems to be a good start. List the Controls in the SortedList and write a custom comparer: //somewhere in the code SortedList myList = new SortedList(yourComparer, this.Controls.Count); foreach(Control ctl in this.Controls){ myList.Add(ctl); } //somewhere else class ControlComparer : IComparer { public bool Compare(object obj1, object obj2){ Control ctl1 = (Control)obj1; Control ctl2 = (Control)obj2; bool returnValue = false; //compare and set [returnValue] return returnValue; } }

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

              Thank you. It turns out that controls have a GetNextControl() property. This avoids the need to recursively iter throught tab controls and frame controls.

              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