Adding Items to ToolStrip at RunTime

吃可爱长大的小学妹 提交于 2019-12-05 08:20:30

If i understand you right, i guess that this is exactly what you want:

    private void buttonAddFav_Click(object sender, EventArgs e)
    {
        ToolStripItem item = new ToolStripMenuItem();
        //Name that will apear on the menu
        item.Text = "Jhon Smith";
        //Put in the Name property whatever neccessery to retrive your data on click event
        item.Name = "GridViewRowID or DataKeyID";
        //On-Click event
        item.Click += new EventHandler(item_Click);
        //Add the submenu to the parent menu
        favToolStripMenuItem.DropDownItems.Add(item);
    }

    void item_Click(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

This is quite simple. You just have to setup a callback method which is used for all favorite ToolStripMenuItem's. In this method you compare the item.Text or item.Name attributes and execute the different favorite methods.

private void FavoriteToolStriptem_Click(object sender, EventArgs e) {
    ToolStripMenuItem item = sender as ToolStripMenuItem;
    MessageBox.Show("You clicked on the menu item called " + item.Name + " shown as " + item.Text);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!