I have a Winform in c# and I’m trying to build a hashtable with a list of buttons that I have. IE: Monday0700Button, Monday0730Button, Monday0800Button, and so on.
In the hashtable I am trying to order these, so that if I need to use a button, and the button that follows it, I can use the hashtable key. IE: if I need to color the background of Monday0730 and Monday 0800, I can somehow tell it to find the next key in the list.
How do I add these buttons into a hashtable so that I can use them in this capacity.
Thanks.
What you need is probably an OrderedDictionary, though other methods are also possible. I wouldn’t go for a Hashtable, because that’s unordered and requires quite a bit more coding to get ordering information in.
Use the following to fill an
OrderedDictionary(I assumed WinForms buttons, but ASP.NET or other buttons would also work):Note: you can add the buttons automatically by looping through all controls.
Then, you can use the following method to find the location of the next ordered item (unfortunately, you cannot get the “next item” without looping, you’ll have to write your own ordered list if you need that):
Finally, use this to get a certain button and retrieve the next one:
The advantage of using an OrderedDictionary is that you shouldn’t worry about how you add the items, they’ll be ordered for you.
Update: slightly expanded explanation