菜单项事件处理程序

Menu item event handler

我正在尝试通过使用右键单击菜单来修改 DataGridView 对象。

目前我可以成功创建右键菜单并确定在选择菜单项后将调用的方法,但是目标方法无法访问有关在 DataGridView 中选择了哪条记录的信息选择了菜单项——或与此相关的任何其他信息(除非它是 class 级变量)。

我的目标是找到一种方法将信息发送到目标方法,或者找到一种方法在创建右键单击菜单的相同方法中修改 DataGridView 对象。

https://msdn.microsoft.com/en-us/library/system.windows.forms.menuitem(v=vs.110).aspx

        private void InspectionDataGridViewCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextMenu m = new ContextMenu();

                if (this.currentInspection != null) // the row that is being right-clicked might not be the row that is selected.
                {
                    m.MenuItems.Add(new MenuItem(string.Format("Mark inspection point {0} as complete.", this.currentInspection.InspectionNumber), this.markInspectionPointComplete));
                }
            m.Show(this.InspectionDataGridView, new Point(e.X,e.Y));
        }
    }

    private void markInspectionPointComplete(object sender, EventArgs e)
    {
        MessageBox.Show("the right-click menu works.");
    }

我尝试使用 DataGridViewCellMouseEventArgs 对象调用目标方法,但这会导致 m.MenuItems.Add() 出错行,因为它只需要一个 EventArgs 对象。

所以,要么我需要修改发送的 EventArgs 对象,找到另一个方法签名来达到这个目的,要么找到一种方法在创建右键单击菜单的同一方法中对 DataGridView 执行操作.

提前致谢!

最简单的解决方案是使用 'MenuItem' 对象的 'Tag' 属性。

private void InspectionDataGridViewCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenu m = new ContextMenu();

        int currentRow = e.RowIndex;
            if (this.currentInspection != null) // the row that is being right-clicked might not be the row that is selected.
            {
                var MI = new MenuItem(string.Format("Mark inspection point {0} as complete.", this.currentInspection.InspectionNumber), this.markInspectionPointComplete)
                MI.Tag = e;
                m.MenuItems.Add(MI);
            }
        m.Show(this.InspectionDataGridView, new Point(e.X,e.Y));
    }
}

private void markInspectionPointComplete(object sender, EventArgs e)
{
    var MI = (MenuItem)sender;
    DataGridViewCellMouseEventArgs Obj = (DataGridViewCellMouseEventArgs) MI.Tag;

    // Do whatever you want with your Obj

    MessageBox.Show("the right-click menu works.");
}

像这样使用 lambda 表达式:

private void InspectionDataGridViewCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenu m = new ContextMenu();

        int currentRow = e.RowIndex;
            if (this.currentInspection != null) // the row that is being right-clicked might not be the row that is selected.
            {
                var MI = new MenuItem(string.Format("Mark inspection point {0} as complete.", this.currentInspection.InspectionNumber));
                MI.Click += (s, x) =>
                {
                    // Use 'e' or 'sender' here
                }
                m.MenuItems.Add(MI);
            }
        m.Show(this.InspectionDataGridView, new Point(e.X,e.Y));
    }
}