5

I have a TableLayoutPanel with 3 columns and 1 row: (Remove button, User Control, Add button)

I want the Add button to add a new row similar to the above below the clicked button: e.g: BEFORE:

  1. (Remove button 1, User Control 2, Add button 1)
  2. (Remove button 2, User Control 2, Add button 2)

After clicking"Add button 1":

  1. (Remove button 1, User Control 2, Add button 1)
  2. (Remove button 3, User Control 3, Add button 3)
  3. (Remove button 2, User Control 2, Add button 2)

I managed to add the row to the end of the tablelayoupanel but not to the middle: It keeps screwing up the layout. Here's a snippet of the event handler:

void MySecondControl::buttonAdd_Click( System::Object^ sender, System::EventArgs^ e )
{
   int rowIndex = 1 + this->tableLayoutPanel->GetRow((Control^)sender);

   /* Remove button */
   Button^ buttonRemove = gcnew Button();
   buttonRemove->Text = "Remove";
   buttonRemove->Click += gcnew System::EventHandler(this, &MySecondControl::buttonRemove_Click);

   /* Add button */
   Button^ buttonAdd = gcnew Button();
   buttonAdd->Text = "Add";
   buttonAdd->Click += gcnew System::EventHandler(this, &MySecondControl::buttonAdd_Click);

   /*Custom user control */
   MyControl^ myControl = gcnew MyControl();

   /* Add the controls to the Panel. */
   this->tableLayoutPanel->RowCount += 1;
   this->tableLayoutPanel->Controls->Add(buttonRemove, 0, rowIndex);
   this->tableLayoutPanel->Controls->Add(myControl, 1, rowIndex);
   this->tableLayoutPanel->Controls->Add(buttonAdd, 2, rowIndex);
}

This doesn't work properly.

Am I doing something wrong? any suggestions?

Eldad
  • 1,067
  • 16
  • 36

1 Answers1

6

Finally found a solution: Instead of adding the controls to thier direct location, I'm adding them to the end and then use the SetChildIndex() function to move the control to the desired location:

void MySecondControl::buttonAdd_Click( System::Object^ sender, System::EventArgs^ e )
{
   int childIndex = 1 + this->tableLayoutPanel->Controls->GetChildIndex((Control^)sender);

   /* Remove button */
   Button^ buttonRemove = gcnew Button();
   buttonRemove->Text = "Remove";
   buttonRemove->Click += gcnew System::EventHandler(this, &MySecondControl::buttonRemove_Click);

   /* Add button */
   Button^ buttonAdd = gcnew Button();
   buttonAdd->Text = "Add";
   buttonAdd->Click += gcnew System::EventHandler(this, &MySecondControl::buttonAdd_Click);

   /*Custom user control */
   MyControl^ myControl = gcnew MyControl();

   /* Add the controls to the Panel. */
   this->tableLayoutPanel->Controls->Add(buttonRemove);
   this->tableLayoutPanel->Controls->Add(myControl);
   this->tableLayoutPanel->Controls->Add(buttonAdd);

   /* Move the controls to the desired location */
   this->tableLayoutPanel->Controls->SetChildIndex(buttonRemove, childIndex);
   this->tableLayoutPanel->Controls->SetChildIndex(myControl, childIndex + 1);
   this->tableLayoutPanel->Controls->SetChildIndex(buttonAdd, childIndex + 2);
}
Eldad
  • 1,067
  • 16
  • 36
  • +1 Thank you so much for sharing, Eldad! I had EXACTLY the same problem. It just didn't make any sense why adding elements at the beginning and the end of the collection worked but failed anywhere between start and end... SetChildIndex not only took away much code but also works like a charm :) thx again! – libjup Aug 27 '12 at 21:06