C#ContextMenuStrip在运行时分配

时间:2018-04-25 02:16:25

标签: c# visual-studio

C# - 在我的应用程序中,我有三个单独的contextSensitiveMenus

  1. popUpGuests
  2. popUpPeople
  3. popUpStatements
  4. 在运行时,我想根据特定条件选择使用哪个popUp menu。我可以在网上找到很多关于在运行时创建contextSensitiveMenus但在选择“预定义”菜单时没有选择的内容。

      

    我一直在摆弄:​​txtText.ContextMenuStrip ......但是无法得到它   工作。

    有什么想法吗?

1 个答案:

答案 0 :(得分:1)

假设您在确定从哪个上下文菜单中选择了哪个选项时遇到问题。

正如我在评论中提到的,我在这里发布了一个可用的示例代码,以便您更好地理解。

首先,您必须创建一个枚举,表示上下文菜单的所有可能的菜单选项。我们将使用它来唯一标记菜单选项。 (您可以通过简单地标记string来避免这种情况,但枚举很简单且干净,可以使用)

假设您有以下选项。

    public enum contextMenuOptions
    {
        guestOption1,
        guestOption2,
        guestOption3,
        peopleOption1,
        peopleOption2,
        peopleOption3,
        statOption1,
        statOption2,
        statOption3
    }

在初始化“预定义”菜单选项时,您可以使用此枚举标记它们,如下所示。此外,您可以将已创建的ContextMenuStrip设置为特定的TextBox

        ContextMenuStrip popUpGuest = new ContextMenuStrip();

        ToolStripTextBox guestMenuitem1 = new ToolStripTextBox();
        guestMenuitem1.Text = "Guest Option1";
        guestMenuitem1.Tag = contextMenuOptions.guestOption1; //tagging with enum
        guestMenuitem1.Click += Menuitem_Click;
        popUpGuest.Items.Add(guestMenuitem1);

        txtGuest.ContextMenuStrip = popUpGuest;

其他选项和其他上下文菜单的方式相同

        ContextMenuStrip popUpPeople = new ContextMenuStrip();
        ToolStripTextBox peopleMenuitem1 = new ToolStripTextBox();
        peopleMenuitem1.Text = "people option1";
        peopleMenuitem1.Tag = contextMenuOptions.peopleOption1;
        peopleMenuitem1.Click += Menuitem_Click;
        popUpPeople.Items.Add(peopleMenuitem1);
        txtPeople.ContextMenuStrip = popUpPeople;

        ContextMenuStrip popUpStatement = new ContextMenuStrip();
        ToolStripTextBox stateMenuitem1 = new ToolStripTextBox();
        stateMenuitem1.Text = "stat Option1";
        stateMenuitem1.Tag = contextMenuOptions.statOption1;
        stateMenuitem1.Click += Menuitem_Click;
        popUpStatement.Items.Add(stateMenuitem1);
        txtStatement.ContextMenuStrip = popUpStatement;

请注意,所有选项的click handler都是常见的Menuitem_Click,因此无论从哪个上下文菜单中选择哪个选项,都会调用此处理程序。在这里,我们将提出一般解决方案。

    void Menuitem_Click(object sender, EventArgs e)
    {
        ToolStripTextBox senderMenuItem = (ToolStripTextBox)sender;
        contextMenuOptions tag = (contextMenuOptions)senderMenuItem.Tag;

        TextBox textBoxToBeUpdated = null;

        string optionSelected = "";
        switch (tag)
        {
            case contextMenuOptions.guestOption1:
                optionSelected = "guestOption1";
                textBoxToBeUpdated = txtGuest;
                break;
            case contextMenuOptions.guestOption2:
                optionSelected = "guestOption2";
                textBoxToBeUpdated = txtGuest;
                break;
            case contextMenuOptions.guestOption3:
                optionSelected = "guestOption3";
                textBoxToBeUpdated = txtGuest;
                break;
            case contextMenuOptions.peopleOption1:
                optionSelected = "PeopleOption1";
                textBoxToBeUpdated = txtPeople;
                break;
            case contextMenuOptions.statOption1:
                optionSelected = "StatmentOption1";
                textBoxToBeUpdated = txtStatement;
                break;
            default:
                break;
        }

        textBoxToBeUpdated.Text = optionSelected;
    }