2017-06-02 147 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