I'm developing a small web browser but am struggling with dynamically creating an eventhandler for a dropdown item in the main menu.

I have a SortedList called items containing two values, items.Key is the site name, like "Google", and items.Value is the URL, "www.google.com". Below is the code I use to create the drop down item. I want it to display item.Key, i.e. the site name, but on click I want it to navigate to Item.Value, the URL.

// This code creates the item so that it displays the item.Key value.
// The last line in this foreach loop creates the eventhandler for
//the new item

foreach (DictionaryEntry item in bookmarks)
            {
                    string name = item.Key + "";
                    if (bookmarksToolStripMenuItem.DropDownItems.ContainsKey(name) != true)
                    {
                        bookmarksToolStripMenuItem.DropDownItems.Add(name);
                        this.bookmarksToolStripMenuItem.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(ClickHandler);
                    }
            }

// This is the event, as far as I am able to do it
// w.Navigate is the method used to navigate to a page
// It accepts a string as input
public void ClickHandler(Object sender, System.EventArgs e)
        {
            w.Navigate(bookmarksToolStripMenuItem.Text.ToString());
        }

Evidently this does not work, as it tries to navigate to the items.Key, or the site name. As of yet I haven't found a solution to this problem.

Does anyone have any ideas?

Take a look,

....
  Dictionary<string, string> data = new Dictionary<string, string>()
            {
                {"A","1"},
                {"B","2"},
                {"C","3"}
            };
        private void Form1_Load(object sender, EventArgs e)
        {
           
            foreach (KeyValuePair<string, string> item in data)
            {
                toolStripDropDownButton1.DropDownItems.Add(item.Key);
            }
                 
            toolStripDropDownButton1.DropDownItemClicked += new ToolStripItemClickedEventHandler(toolStripDropDownButton1_DropDownItemClicked);
 
        }

        void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            MessageBox.Show(data[e.ClickedItem.Text]);
        }

Thanks for the reply, although it doesn't solve my problem. I want to use the item.Key value as the text of the DropDownItem, then I want to use the item.Value value in the method, or in this case, the MessageBox.Show clause. Thus e.ClickedItem.Text wont work since it is equivalent to the item.Key value.

Another problem I face is that this code

Dictionary<string, string> data = new Dictionary<string, string>()
            {
                {"A","1"},
                {"B","2"},
                {"C","3"}
            };
        private void Form1_Load(object sender, EventArgs e)
        {
 
            foreach (KeyValuePair<string, string> item in data)
            {
                toolStripDropDownButton1.DropDownItems.Add(item.Key);
            }
 
            toolStripDropDownButton1.DropDownItemClicked += new ToolStripItemClickedEventHandler(toolStripDropDownButton1_DropDownItemClicked);

is already in a click method. For reasons I don't fully understand (yet) I can't add this code -->

void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            MessageBox.Show(data[e.ClickedItem.Text]);
        }

inside the click method, making accessing the dynamic menu item all the more difficult

Why can you not add the code? Can you be more specific?
Have you used a toolStripDropDown item or is it a different control?

OK sorry about that, here is my full code.

private void DoBookmarks{
SortedList bookmarks = new SortedList(1);
// Other code here that populates the sorted list from a database
foreach (DictionaryEntry item in bookmarks)
            {
                if (bookmarksToolStripMenuItem.DropDownItems.ContainsKey(item.Value.ToString()) != true)
                {
                    bookmarksToolStripMenuItem.DropDownItems.Add(item.Value.ToString());
                    this.bookmarksToolStripMenuItem.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(ClickHandler);
                }
            }
        }

        void ClickHandler(Object sender, System.EventArgs e)
        {
            w.Navigate(bookmarksToolStripMenuItem.Text);
        }

From the ClickHandler() method I can't access the SortedList since it exists within the DoBookmarks() method only. The items the click method is for drop down items in a ToolStripMenuItem. I want to use the SortedList.Key value as the text for the drop down items and the SortedList.Value value for the w.Navigate method's input.

Since I can't access the SortedList from outside the DoBookmarks() method this proves difficult, and I can't create the ClickHandler() method inside the DoBookmarks() method, obviously.

I've added a small screenshot, not sure if it will help though.

So move the sorted list outside the method. Make it a private class member so that you can access it from your click handler method :)

I tried that but I'm struggling to isolate the item that was clicked. So I'm using this code -

SortedList bookmarks = new SortedList(1);
private void DoBookmarks{
// Other code here that populates the sorted list from a database
foreach (DictionaryEntry item in bookmarks)
            {
                if (bookmarksToolStripMenuItem.DropDownItems.ContainsKey(item.Value.ToString()) != true)
                {
                    bookmarksToolStripMenuItem.DropDownItems.Add(item.Value.ToString());
                    this.bookmarksToolStripMenuItem.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(ClickHandler);
                }
            }
        }
 
        void ClickHandler(Object sender, System.EventArgs e)
        {
           w.Navigate(bookmarks[e.ClickedItem.Text]);
        }

I got the e.ClickedItem code from the post by adatapost

Dictionary<string, string> data = new Dictionary<string, string>()
{
{"A","1"},
{"B","2"},
{"C","3"}
};
private void Form1_Load(object sender, EventArgs e)
{

foreach (KeyValuePair<string, string> item in data)
{
toolStripDropDownButton1.DropDownItems.Add(item.Key);
}

toolStripDropDownButton1.DropDownItemClicked += new ToolStripItemClickedEventHandler(toolStripDropDownButton1_DropDownItemClicked);

}

void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
MessageBox.Show(data[e.ClickedItem.Text]);
}

Which generates the following error
Error 3 'System.EventArgs' does not contain a definition for 'ClickedItem' and no extension method 'ClickedItem' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

The reason for this is obvious, but can someone please explain the statement to me and how I may get it to work? I don't know where the

e.ClickedItem.Text

comes from in the code and thus I'm unsure of how to implement this in my own.

The e.ClickedItem is from the ToolStripItemClickedEventArgs class. The example adatapost gave you used a dropdownitem on a toolstrip rather than a combobox/menu on the menustrip.
The menustrip only uses the base EventArgs class which doesnt contain a ClickedItem property.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.