【问题标题】:Adding function to items in contextmenustrip Windows Forms向 contextmenustrip Windows 窗体中的项目添加功能
【发布时间】:2021-04-30 16:10:45
【问题描述】:

我正在使用下面的代码在 Windows 窗体中创建动态按钮。我希望在右键单击按钮时弹出一个带有不同选项的上下文菜单。我制作了上下文菜单strp,它显示了,但我不知道如何让不同的选项执行我为它们编写的功能。知道如何将这些项目与它们的功能联系起来吗?

void AddButton(Shape s){
            Button b = new Button();
            b.Text = s.name;
            b.Width = sceneItemsList.Width-6;
            b.TabStop = false;
            b.FlatStyle = FlatStyle.Flat;
            b.BackColor = Color.LightGray;
            b.FlatAppearance.BorderColor = Color.Black;
            ContextMenuStrip cm = new ContextMenuStrip();
            cm.Items.Add("Deselect");
            cm.Items.Add("Edit");
            if (s is GroupShape)
            {
                cm.Items.Add("Ungroup");
            }
            cm.Items.Add("Delete");
            b.ContextMenuStrip = cm;
}

【问题讨论】:

    标签: c# winforms button contextmenustrip


    【解决方案1】:

    想通了!如果其他人为此苦苦挣扎,这是我的代码。基本上,您创建一个遍历所有选项并添加点击事件处理程序的循环

        cm.Items[0].Click += (sender, e) => {
            Console.WriteLine("ok");
        };
    

    【讨论】:

      猜你喜欢
      • 2016-04-19
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      相关资源
      最近更新 更多