English 中文(简体)
C# Building An Array with Button Entries
原标题:

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.

最佳回答

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.

问题回答

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

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.

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.

You can create an array with existing buttons like this.

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

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签