0

Some quick context: in this project we're using a Visual C# Windows Form project to recreate Minesweeper.

I am using an array of Cells (which inherit from Control.Button).

As extra credit, I want the user to be able to flag a cell like you can in the class version of the game. However, I can't get right-clicking to work.

When trying to find a solution, I read that you need to typecast the EventArg as a MouseEventArg, but that didn't solve my problem as right-clicking doesn't even trigger my click event.

Here's some paraphrased code:

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
            }
        } 
    }
}

This is where I grabbed the type-casting the event arguments from.

Community
  • 1
  • 1
Jack Thias
  • 70
  • 1
  • 9

3 Answers3

1

MouseClick does not handle right clicks for buttons. You can use MouseDown.

board[i, j].MouseDown += button_arrayClick; 
John Koerner
  • 37,428
  • 8
  • 84
  • 134
1

Use the _MouseDown event instead.

    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

Stavm
  • 7,833
  • 5
  • 44
  • 68
  • This would work normally, but since I'm use an array of buttons that aren't displayed in my design view, I have to use the method @JohnKoerner suggested. – Jack Thias May 02 '16 at 19:42
0

listen for MouseDown event

private void button1_MouseDown(object sender, MouseEventArgs e)
Kiran Paul
  • 875
  • 6
  • 18