【问题标题】:How to get the control which opened the ContextMenuStrip?如何获取打开 ContextMenuStrip 的控件?
【发布时间】:2014-05-27 16:53:26
【问题描述】:

我正在寻找一种方法来为程序提供导致 ContextMenuStrip 打开的控件。我正在为多个组合框分配相同的条以重复使用它们,就像在课程开始时这样。

myComboBox.ContextMenuStrip = changevaluestrip;

这里我有“添加值”和“删除值”,当然它们中的每一个都必须知道它必须从哪个组合框中删除值。我试着用

private void removeValueToolStrip_Click(object sender, EventArgs e)
{
    ToolStripMenuItem usedbox = sender as ToolStripMenuItem;
    var parent = usedbox.GetCurrentParent();
    DialogResult res = MessageBox.Show("Do you really want to delete this value?", "Delete Value", MessageBoxButtons.YesNo);
    if (res == DialogResult.Yes)
    {
        //Delete it from the combobox it was sent from
    }
}

但这并没有真正奏效,只是给了我“删除值”作为发件人...

【问题讨论】:

    标签: c# combobox contextmenustrip


    【解决方案1】:

    也许ContextMenuStrip.SourceControl 是你想要的。

    【讨论】:

      【解决方案2】:

      是的,@KingKing 是对的。为此,您需要使用ContextMenuStrip.SourceControl。 这是一个为您执行此操作的 sn-p。

      ToolStripMenuItem usedbox = sender as ToolStripMenuItem;
      ContextMenuStrip parent = usedbox.GetCurrentParent() as ContextMenuStrip;
      if (parent != null)
      {
          ComboBox combo = parent.SourceControl as ComboBox;
          if (combo != null)
          { 
               //use combobox here   
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-13
        • 1970-01-01
        相关资源
        最近更新 更多