2017-06-02 22 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."); 
    } 

Iは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

使用ラムダ式は:

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

は当初、それがうまくいくかもしれない "タグ" ソリューションのように見えました。しかし、Tagの割り当ては適切ではないようです。 ********************************************************************************************************************************************** ラムダ式は、書かれたとおりに動作するように見えます。 – Sal

関連する問題