tags:

views:

135

answers:

6

I have a set of buttons that I want to add into an array so that they are ordered. The buttons I have are:

Monday0700Button
Monday0730Button
Monday0800Button
Monday0830Button

and so on.

How do I add a button into an array, and have them ordered, so that I can use this order later on for different uses.

Thanks.

A: 

SortedList<TKey, TValue> should do the trick. Where TKey is the property of the button you wish to order on and TValue is your Button type.

Si
A: 

You simply create an ordered collection of buttons that is for example:

List<Button> lst

If the order that you add those elements is not the one you want it to be you can use the method Sort().

If you want to keep additional information associated with a button then use its Tag property and make use of it while sorting.

kubal5003
A: 

You can create an array with existing buttons like this.

var array = new[] {Monday0700Button,Monday0730Button,Monday0800Button,Monday0830Button};
Rohan West
A: 

How are you defining your order? If your order is defined by "the order with which you set up the array", then the array (or a List), is good enough.

If you want a different ordering than what you are starting with, then you can look at sorting.

Nader Shirazie
+1  A: 

Sounds like a SortedDictionary<string, Button> would fill the bill.

SortedDictionary<string, Button> buttons 
                 = new SortedDictionary<string, Button>();
buttons.Add(btn1.Name, btn1);
buttons.Add(btn2.Name, btn2);

foreach (string name in buttons.Keys)
{
  Button b = buttons[name];
  // iterates in name order
}

Alter the key you use depending on what you're choosing to sort on.

Michael Petrotta
+1  A: 

You can put them all in a list and then sort by ID:

List<Button> buttonList = new List<Button>();
buttonList.Add(Monday0700Button);
buttonList.Add(Monday0730Button);
buttonList.Add(Monday0800Button);
buttonList.Add(Monday0830Button);
buttonList.Sort((l,r) => l.ID.CompareTo(r.ID));
Mun