0

I have the code that creates a grid of buttons from an array. I need to get their possitions in array on mouse clicked event. Any ideas or links to some other post/articles would be very helpful.

The code for creating the grid:

    // Creating buttons array for 5x5 grid
    Button[] tiles25 = new Button[25];

    // Generating 5x5 button grid
    void Spawn5x5Grid()
    {
        // position of the firts tile
        int x = 35, y = 55;

        // current tile index
        int count = 0;

        for (int i = 1; i < 6; i++)
        {
            for (int j = 1; j < 6; j++)
            {
                // Adding button to the array
                tiles25[count] = new Button()
                {
                    Size = new Size(24, 24),
                    Location = new Point(x, y)
                };
                // Adding buttons from array to the form
                Controls.Add(tiles25[count]);
                count++;
                x = x + 24;
            }
            x = 35;
            y = y + 24;
        }
        lblSize.Text = "5 x 5";
        currentGrid = Grids.grid5x5;
    }         
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • So, the first button is placed at x = 35, each one has width = 24, and all of them are attached with no blank spaces. Am I right? – Andrea Jul 27 '18 at 13:46
  • Well, you'd need to add an event handler for the [Click event](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.click(v=vs.110).aspx) of the button, which will get the clicked button passed to it as `sender` parameter. What have you tried so far, and where exactly are you stuck? (I do not see any Click event handler in your code yet)... – bassfader Jul 27 '18 at 13:46
  • Do your buttons move somehow afterward? – L_J Jul 27 '18 at 13:48
  • @Andrea ジーティーオー Yup – VoidWalker Jul 27 '18 at 13:49
  • @bassfader So far I'm just looking for solutions. I haven't tried anything specific yet. – VoidWalker Jul 27 '18 at 13:50
  • @L_J No, the position of the buttons is not changeing. – VoidWalker Jul 27 '18 at 13:51

4 Answers4

1

I suggest scanning tiles25 array in the Click event handler

  ...
  Controls.Add(tiles25[count]);     

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

    int index = Array.IndexOf(tiles25, button);

    //TODO: Put relevant code here: "button" clicked which is at "index" position
  };

  count++;
  x = x + 24;
  ...
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You need to set up an event handler for when the button is clicked. Right now, all you're doing is creating the buttons and adding them to the list of controls at a given position. Now, all you have to add is the event handler for the click event!

//...  
// Adding button to the array
tiles25[count] = new Button()
{
    Size = new Size(24, 24),
    Location = new Point(x, y),
};
tiles25[count] += new EventHandler(this.Tile_Click);
//...

void Tile_Click(Object sender, EventArgs e)
{
    Button clickedButton = (Button)sender;
    //...
}

Then inside of the Tile_Click() event handler, you can use whatever code necessary to get the position with the clickedButton object.

Chris
  • 2,254
  • 8
  • 22
0

Register an event handler for each of your button click events and then extract the Location property from the sender object:

// Generating 5x5 button grid
void Spawn5x5Grid()
{
    // position of the firts tile
    int x = 35, y = 55;

    // current tile index
    int count = 0;

    for (int i = 1; i < 6; i++)
    {
        for (int j = 1; j < 6; j++)
        {
            // Adding button to the array
            tiles25[count] = new Button()
            {
                Size = new Size(24, 24),
                Location = new Point(x, y)
            };
            // Adding buttons from array to the form
            Controls.Add(tiles25[count]);

            tiles25[count].Click += Tiles25_Click;
            count++;
            x = x + 24;
        }
        x = 35;
        y = y + 24;
    }
    lblSize.Text = "5 x 5";
    currentGrid = Grids.grid5x5;
}

private void Tiles25_Click(object sender, EventArgs e)
{
    var bt = sender as Button;

    MessageBox.Show("X = " + bt.Location.X + "; Y = " + bt.Location.Y);
}
L_J
  • 2,351
  • 10
  • 23
  • 28
0

If I'm not wrong VoidWalker, you are trying to get the position(index) of the item in the source array and not the actual position of the button on screen. If the former case is true read on, for the latter we have some good answers above.

What you need to do is to mark each button with an identifier that would be used to infer the position. A simple yet damn efficient approach.

At the time of creating the button:

            // Adding button to the array
            tiles25[count] = new Button()
            {
                Size = new Size(24, 24),
                Location = new Point(x, y)
            };
            // Add the current index to the name field of the Button
            tiles25[count].Name = "Grid5-Btn" + count.ToString();
            // Adding buttons from array to the form
            Controls.Add(tiles25[count]);

Then on button click you can simply do

void Tile_Click(Object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
var index = int(clickedButton.Name.Split("Grid5-Btn")[0]);
   //...
}

This way you can add multiple pieces of informatio such as the hierarchy of grids on the page. You can exactly pinpoint which element to access without running any loops, which would be the case with Array.IndexOf