在运行时添加项目的ToolStrip [英] Adding Items to ToolStrip at RunTime

查看:219
本文介绍了在运行时添加项目的ToolStrip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个ToolStripMenu有收藏菜单,我想在运行时我的WinForms应用程序过程中添加的子项。我有一个DataGridView,我右击表明,有一个添加到收藏夹选项的上下文菜单。当该事件被触发,我想从选定行从datagriview添加使用,也许一些文本的项目(这个我知道该怎么做已经)到这个收藏夹菜单。最棘手的部分是到我需要为我的 newlyCreatedToolStripMenuItem_Click 事件代码。我决定以后怎么救我的收藏夹列表

Hello I have a ToolStripMenu with a "Favorites" menu that I want to add sub items to during my WinForms app at run time. I have a datagridview that I right click on to show a context menu that has an "Add to Favorites" option. When that event is fired, I'd like to add an item using maybe some text from the selected row from the datagriview (this I know how to do already) to this Favorites menu. The tricky part to is I need to create code for my newlyCreatedToolStripMenuItem_Click event. I will determine how to save my favorites list later.

所以,我们打算为:

右键单击为约翰·史密斯

Right click datagridview row for "John Smith"

选择添加到收藏夹从文本菜单行/ code>

Choose "Add to Favorites" from ContextMenu

收藏 ToolStripMenu 有新的产品加入到它,上面写着约翰·史密斯

The Favorites ToolStripMenu has a new item added to it that reads "John Smith"

点击约翰·史密斯 ToopStripMenuItem 触发一个动作(如daragridview行中选择该行,等。)

Clicking the "John Smith" ToopStripMenuItem fires an action (such as select that row in the daragridview row, etc.)

任何好的起点想法?

推荐答案

如果我理解你的权利,我想这是你想要什么:

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();
    }

这篇关于在运行时添加项目的ToolStrip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆