0

The following code is responsible for creating a 5 by 5 grid of buttons. Thanks to people help, i could insert lambda expretion that will pass an information 'which button from the grid is pressed' to other functions. The thing is, that i need it to distinguish wheter left or right mouse button is pressed and i have no clue how to do that. Any hints or links to other posts/articles would be very helpfull.

    Button[] grid5x5 = new Button[25];
    void Spawn5x5Grid()
    {
        // Position of the first button 
        int x = 50, y = 150;
        int count = 0;
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                grid5x5[count] = new Button
                {
                    Size = new Size(31,31),
                    Location = new Point(x, y)
                };
                this.Controls.Add(grid5x5[count]);

                grid5x5[count].Click += (o, ee) =>
                {
                    Button button = o as Button;

                    // index variable contains the position of the button in 
                    // array
                    int index = Array.IndexOf(grid5x5, button);                        


                };

                count++;
                x = x + 31;
            }
            x = 50;
            y = y + 31;
        }
    }
juharr
  • 31,741
  • 4
  • 58
  • 93
  • You really should include what type of UI this is, web, WPF, winforms? The information is either in the event arguments `ee` or it's possible that a right click doesn't even trigger the event. – juharr Jul 30 '18 at 14:07
  • @juharr it's winform .NET framework – VoidWalker Jul 30 '18 at 14:12
  • @juharr You're right. At this moment it is triggering only on left mouse button. – VoidWalker Jul 30 '18 at 14:14
  • 2
    As clearly specified in [the documentation of Control.Click](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.click(v=vs.110).aspx), if you need to know which button was pressed, use [the MouseClick event](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseclick(v=vs.110).aspx). – Kenneth K. Jul 30 '18 at 14:15
  • @KennethK. I think that solves the problem. Thanks! – VoidWalker Jul 30 '18 at 14:53

0 Answers0