0

I am creating a Visual c# version of minesweeper and have created an array which without identifying a left or right click both work. Obviously in minesweeper there needs to be a right click for flag and a left click to clear. I started by developing the clear with just the .Click and that works but when I call upon the mouseButton to click with a left or a right this does not work.

I tried moving around the order of the IF but this causes other errors within my code so I was hoping I could gain some advice on how to rewrite my code to make it work? Here's my entire section with the mouse information inside.

for (int a = 0; a < BoardSize; a++)
            {
                for (int d = 0; d < BoardSize; d++)
                {
                    cov[a, d] = new PictureBox();
                    cov[a, d].Height = 30;                              //Same size as the Hid pictureboxes
                    cov[a, d].Width = 30;
                    cov[a, d].Left = (a * 40) + 25;
                    cov[a, d].Top = (d * 40) + 25;
                    cov[a, d].BackColor = Color.Plum;               //Colour of the cover layer
                    cov[a, d].BringToFront();
                    cov[a, d].Name = a + d.ToString();
                    this.Controls.Add(cov[a, d]);
                    if (e is MouseEventArgs)
                    {
                        MouseButtons mouseButton = (e as MouseEventArgs).Button;

                        if (mouseButton == MouseButtons.Left)
                        {
                            cov[a, d].Click += leftClick;                  
                        }
                        else if (mouseButton == MouseButtons.Right)
                        {
                            MessageBox.Show("Right mouse button is clicked");
                        }

                    }
                    
                }
            }
Harry
  • 1
  • 1
  • You are only subscribing to .CLick everytime you press the left mouse button. You should execute it's logic here. – Marvin Klein Dec 12 '22 at 09:56
  • Does this answer your question? [How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?](https://stackoverflow.com/questions/19448346/how-to-get-a-right-click-mouse-event-changing-eventargs-to-mouseeventargs-cause) – Standard_101 Dec 12 '22 at 10:22

1 Answers1

2

You should init your Board first. For example:

for (int a = 0; a < BoardSize; a++)
{
    for (int d = 0; d < BoardSize; d++)
    {
        cov[a, d] = new PictureBox();
        cov[a, d].Height = 30;                              //Same size as the Hid pictureboxes
        cov[a, d].Width = 30;
        cov[a, d].Left = (a * 40) + 25;
        cov[a, d].Top = (d * 40) + 25;
        cov[a, d].BackColor = Color.Plum;               //Colour of the cover layer
        cov[a, d].BringToFront();
        cov[a, d].Name = a + d.ToString();
        cov[a, d].Click += Field_Click;                  
        this.Controls.Add(cov[a, d]);
    }
}

Notice how I subscribe to the Click event of the picturebox. This is being handled within a seperate function like this:

private void Field_Click(object sender, EventArgs e)
{
    if (e is MouseEventArgs mouseEvent)
    {
        if (mouseEvent.Button == MouseButtons.Left)
        {
            // DO YOUR LEFT CLICK LOGIC
        }
        else if (mouseEvent.Button == MouseButtons.Right)
        {
            // DO YOUR RIGHT CLICK LOGIC
        }
    }
}
Marvin Klein
  • 1,436
  • 10
  • 33