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