简体   繁体   English

MenuItem Click事件处理程序未调用

[英]MenuItem Click event handler not called

I´m building a ContextMenu on the fly, like this 我正在动态构建一个ContextMenu,就像这样

            readinstance = null;
            ContextMenu cMenu = new ContextMenu();
            for (int i = 0; i < instances.Length; i++) {
                string text = String.Format("{0} - {1}", instances[i].Id, instances[i].FormName);
                MenuItem item = new MenuItem(text, new EventHandler(cMenuitem_Click));
                item.Tag = instances[i];
                cMenu.MenuItems.Add(item);
            }
            cMenu.Show((Button)sender, new Point(0, 0));
            cMenu.Dispose();
            if (readinstance == null)
                throw new Exception("Must select some instance");

and the handler is 而处理程序是

    void cMenuitem_Click(object sender, EventArgs e)
    {
        MenuItem item = (MenuItem)sender;
        readinstance = (FormPrintingStorage)item.Tag;
    }

The menu displays correctly, but when I click some of the options, the handler is not called, so readinstance remains null, and the exception throws. 菜单正确显示,但是当我单击某些选项时,未调用处理程序,因此readinstance保持为null,并引发异常。 As a side note, when I click any of the options, the menu disappears. 附带说明,当我单击任何选项时,菜单都会消失。 I cannot see what is wrong with my code. 我看不到代码有什么问题。 Any help will be appreciated. 任何帮助将不胜感激。

I´m answering my own question, because I tried more ways. 我在回答自己的问题,因为我尝试了更多方法。 The first one was to replace the ContextMenu with a ListView and an "Ok" button, at no luck, because the wait loop needed a Thread.Sleep. 第一个方法是用ListView和“ Ok”按钮替换ContextMenu,但运气不好,因为等待循环需要Thread.Sleep。 No comments. 没意见。

The solution was to implement a new dialog with an empty list view an the Ok button. 解决方案是使用空列表视图和“确定”按钮实现一个新对话框。 Some of the relevant code follows. 一些相关的代码如下。 Note that only TreeViewItem/s are moved between the main form and the dialog. 请注意,只有TreeViewItem在主窗体和对话框之间移动。

    ListViewItem _result = null;

    public ListViewItem Result { get { return _result; } }

    public List<ListViewItem> Source
    {
        set
        {
            listView1.Items.Clear();
            foreach (ListViewItem item in value)
                listView1.Items.Add(item);
            listView1.View = View.List;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (_result == null)
            return;
        DialogResult = DialogResult.OK;
        Close();
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListView list = (ListView)sender;
        ListView.SelectedIndexCollection indices = list.SelectedIndices;
        if (indices.Count == 0)
            return;
        _result = list.Items[indices[0]];

    }

Getting the Result, the main form may do anything it wants with the Tag member. 获取结果后,主窗体可以对Tag成员执行任何所需的操作。 In fact, I´m using the same dialog for two different purposes in the same form. 实际上,我将同一对话框用于同一形式的两个不同目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM