VoidWalker
VoidWalker

Reputation: 1

How to get position of a button in an array on mouse clicked

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

Upvotes: 0

Views: 825

Answers (4)

Vikrant Siwach
Vikrant Siwach

Reputation: 70

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

Upvotes: 0

Chris
Chris

Reputation: 2304

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.

Upvotes: 0

L_J
L_J

Reputation: 2437

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

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

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;
  ...

Upvotes: 1

Related Questions