开发者

C# Insert Button Into Hashtable

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.开发者_Go百科 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):

// I assume your buttons are created somewhere
// or already exist, i.e., they look something like:
Button MondayXXXButton = new Button();
MondayXXXButton.Name = "MondayXXXButton";
MondayXXXButton.Text = "Hello world";

OrderedDictionary buttons = new OrderedDictionary();
buttons.Add(MondayXXXButton.Name, MondayXXXButton);
// etc: for each button

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

Button GetNextButton(OrderedDictionary buttons, string searchThis)
{
    IEnumerator enumerator = buttons.Keys.GetEnumerator();

    for (enumerator.Reset(); enumerator.MoveNext(); )
        if (enumerator.Current == searchThis)
            break;

    // if false, nothing was found, or last item was found
    if (enumerator.MoveNext())
        return (Button)enumerator.Current;
    else
        return null;
}

Finally, use this to get a certain button and retrieve the next one:

Button someButton = (Button) buttons["MondayYYYButton"];
Button nextButton = GetNextButton(buttons, "MondayYYYButton");

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


Hashtables have no inherent order. There is no "next" key in a standard hashtable.

You actually used the name of the data structure that you probably should use: List. Lists will enable you to go "forward" and "backward" as needed.


This is difficult to answer without more information, but, based on what you have here, the first thing that comes to mind is this. If you set the IDs of your buttons to Monday0700Button or Monday0730Button, add just those strings to a list.

List<string> myButtons = new List<string>();

myButtons.Add( "Monday0700Button" );
myButtons.Add( "Monday0730Button" );
myButtons.Add( "Monday0800Button" );
myButtons.Add( "Monday0830Button" );

Then when you need to use one, use 'FindControl' to find the instance of the button using the name.

int index = myButtons.IndexOf( clickedButtonId );
FindControl( myButtons[index++] );

You would not want to put instances of the buttons into a Hashtable/dictionary, I would not think.


A doubly-linked list would be more apropriate data structure for both forward and backward iteration where each list item serves as a data source for your buttons (e.g. left-wise button would traverse to the left node, and the right-wise button would traverse to the right node). It doesn't really make sense to store buttons in the data structure at this point in the discussion, but instead store information which describes each node so that static buttons can have their text updated appropriately.

You can also achieve this using a List, however, it would require more code within the presentation tier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜