2016-05-02 22 views
0

いくつかのクイックコンテキスト:このプロジェクトでは、Visual C#Windowsフォームプロジェクトを使用してMinesweeperを再作成しています。Windowsフォームボタンのアクションを右クリック

私はCell(Control.Buttonを継承しています)の配列を使用しています。

余分なクレジットとして、私はあなたがゲームのクラスバージョンでできるように、セルにフラグを立てることができるようにします。しかし、私は右クリックして作業することはできません。

解決策を見つけようとすると、EventArgをMouseEventArgとして型キャストする必要があることがわかりましたが、右クリックをしてもクリックイベントが発生しなくなるため、問題は解決しませんでした。私はからtype-casting the event argumentsをつかんだところ

namespace Project_5___Minesweeper_GUI 
{ 
    public partial class Form1 : Form 
    { 
     public class Cell : Button { /*Custom Cell-Stuff Goes Here*/ } 

     Cell[,] board = new Cell[AXIS_LENGTH, AXIS_LENGTH]; //Axis Length is just the dimensions of the board (I use 10x10). 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      for (int i = 0; i < AXIS_LENGTH; i++) 
      { 
       for (int j = 0; j < AXIS_LENGTH; j++) 
       { 
        board[i, j] = new Cell(); 
        //Set position and size 
        board[i, j].MouseClick += button_arrayClick; //button_arrayClick() is never called by a right-click. Code for it is below. I suspect this line of code has to do with right-clicks not class button_arrayClick(). 
        groupBox1.Controls.Add(board[i, j]); //I'm containing the array of Cells inside of a groupbox. 
       } 
      } 
     } 

     private void button_arrayClick(object sender, EventArgs e) //Is prepared for handling a right-click, but never receives them. 
     { 
      Cell temp = (Cell)sender; 
      MouseEventArgs me = (MouseEventArgs)e; 
      if (me.Button == MouseButtons.Left) 
      { 
       //Stuff that happens on left-click 
      } else { 
       //Stuff that happens on right-click 
      } 
     } 
    } 
} 

これは、次のとおりです。

はここにいくつかの言い換えのコードです。

答えて

1

MouseClickボタンの右クリックは処理されません。 MouseDownを使用できます。

board[i, j].MouseDown += button_arrayClick; 
+0

感謝を聞いてくれ!このソリューションは機能し、私は今、私のセルのために機能する右クリックを持っています。 –

1

代わりに_MouseDownイベントを使用してください。

private void button1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Right) 
     { 
      //stuff that happen on right-click 
     } 
     else if (e.Button == System.Windows.Forms.MouseButtons.Left) 
     { 
      //stuff that happen on left click 
     } 
    } 

auto generate handler in Visual Studio 2013

+0

これは正常に動作しますが、デザインビューに表示されないボタンの配列を使用しているため、@ JohnKoernerが提案するメソッドを使用する必要があります。 –

0

は、MouseDownイベントのために

private void button1_MouseDown(object sender, MouseEventArgs e) 
関連する問題