user1097772
user1097772

Reputation: 3529

Visual Studio 2010 TableLayoutPanel with Buttons events


I've made in Visual Studio 2010 in designer mode a TableLayoutPanel (for example 8x8 rowsxcolumns), then I've put in each field of this "matrix" a button. Is there any way how to handle the events centrally? I found that TableLayoutPanel have TabIndexChanged, but I don't know if this will be useful for me.
I'm just trying to find out if is possible this:
I click on second button and get the information on what button I've clicked and will be able to change some of his property - for example icon, or text.
Lets imagine an idea that I will understand tablelayoutpanel as an "matrix" or "array" and I want to work with it something like this:

1)I'll click on button on possition i (or i,j)
2)I'll change the Icon of button on possition i (or i,j)

Is that possible? If yes, how?
Thx

Upvotes: 0

Views: 2352

Answers (2)

Conrad Frix
Conrad Frix

Reputation: 52645

You can have a single method handle all your button click events and then use the sender parameter to modify the image on the button.

e.g.

private void CreateButtons(object sender, EventArgs e)
    {
        for (int i = 0; 8 < length; i++)
        {

            for (int j = 0; 8 < length; i++)
            {
                var btn = new Button() { Text = "Something" };
                btn.Click += new EventHandler(btn_Click);
                this.tableLayoutPanel1.Controls.Add(btn, i, j);
            }
        }

    }

    void btn_Click(object sender, EventArgs e)
    {
         ((Button)sender).Image= SomeMethodThatReturnsAnImage();
    }

As an aside the TabIndexChanged would be completely useless to you since its only raised when the TabIndex property of a control gets changed which is typically very rare. You'll rember that the TabIndex property is used to control the order of controls for when the user presses the Tab key.

Upvotes: 1

ssis_ssiSucks
ssis_ssiSucks

Reputation: 1506

Sounds like you just need to associate the click event of all your buttons with a single event handler, and then manipulate the clicked button, etc., inside the event handler

Upvotes: 0

Related Questions