2017-06-02 131 views
0

我想通过使用右键单击菜单来修改DataGridView对象。菜单项事件处理程序

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

我的目标是找到一种方法来将信息发送到目标方法,或者找到一种方法来修改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对象调用目标方法,但因为它仅期望一个EventArgs的创建与m.MenuItems.Add()线的误差目的。

所以,无论我需要修改发送的EventArgs对象,查找另一个将用于此目的的方法签名,或者找到一种方法在创建右键单击菜单的相同方法内对DataGridView执行操作。

在此先感谢!

回答

1

最简单的解决方案是使用'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."); 
} 

OR

使用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)); 
    } 
} 
+0

最初,它看起来像 “变量” 的解决方案可能会奏效。但是,标签的分配似乎并不合适。我试了一个新的行,没有大括号,(*** MI.Tag = e ***),它的工作方式与我的预期一致。 lambda表达式看起来像它会像写入一样工作。 – Sal