Add child items to ToolStripMenuItem using C# – C Sharp

I wanted to be able to populate menu items from a database, i.e. I had a Print Document top level menu item and I wanted to be able to populate the children from the database dynamically with each available document.

The theory was this:

1. Use the MouseHover event for the main menu item
2. When the user hovers over the item, get the list of documents and add them as children
3. Add a Click event handler to process clicks on the children

Below is the MouseOver code (NB I’ve replaced the load from database code with three simple allocations to simplify the code). Notice that the property that holds the menu item children is DropDownItems.

private void MyToolStripMenuItem_MouseHover(object sender, EventArgs e)
{
    //create tsParent using sender
    ToolStripMenuItem tsParent = (ToolStripMenuItem)sender;
    

    //check to see if the children have already been created 
    //(stops them being added repeatedly if you hover more than once)

    if (tsParent.DropDownItems.Count == 0)
    {
        //define new ToolStripMenuItem to use as the new child
        ToolStripMenuItem newChild;

        //in the real case this is a loop adding items from a database
        //notice the Click event handler added to each item detailed below
        newChild= new ToolStripMenuItem();
        newChild.Text = "Added Item One";
        newChild.Tag = 1;
        newChild.Click += new EventHandler(processMeunItem);
        //add the new child to the parent DropDownItems collection
        tsParent.DropDownItems.Add(newChild);

        newChild= new ToolStripMenuItem();
        newChild.Text = "Added Item 2";
        newChild.Tag = 2;
        newChild.Click += new EventHandler(processMeunItem);
        tsParent.DropDownItems.Add(newChild);

        newChild= new ToolStripMenuItem();
        newChild.Text = "Added Item 3";
        newChild.Tag = 3;
        newChild.Click += new EventHandler(processMeunItem);
        tsParent.DropDownItems.Add(newChild);
    }
}

The code above will add three children to the menu item when you hover over it. Now we need to do something when a child is clicked:

//code runs when any of the new children are clicked
private void processMeunItem(object sender, EventArgs e)
{
    //Get the tag of the selected menu item and assign to integer selectedMenuTag
    int selectedMenuTag = Convert.ToInt32(((ToolStripMenuItem)sender).Tag);

    //do something with the selected item...
}

This will allocate the Tag of the clicked item to selectedMenuTag, you can then use it for further processing.

Advertisements
This entry was posted in Programming and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s