c# – 在RunTime中向ToolStrip添加项目

您好我有一个带有“收藏夹”菜单的ToolStripMenu,我想在运行时在WinForms应用程序中添加子项.我有一个datagridview,我右键单击以显示具有“添加到收藏夹”选项的上下文菜单.当该事件被触发时,我想使用datagriview中的所选行中的一些文本添加一个项目(我知道该怎么做)到这个收藏夹菜单.棘手的部分是我需要为我的newlyCreatedToolStripMenuItem_Click事件创建代码.我将在稍后确定如何保存我的收藏夹列表.

所以我们要去:

右键单击“John Smith”的datagridview行

从ContextMenu中选择“添加到收藏夹”

收藏夹ToolStripMenu添加一个新项目,内容为“John Smith”

单击“John Smith”ToopStripMenuItem将触发一个操作(例如在daragridview行中选择该行等)

有什么好的开始想法?

解决方法

如果我理解你的话,我猜这正是你想要的:
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();
    }

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...