0

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.

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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));

Comments

0

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.

Comments

0

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.

Comments

0

You can create an array with existing buttons like this.

var array = new[] {Monday0700Button,Monday0730Button,Monday0800Button,Monday0830Button};

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.