2011-11-01 21 views
5

Tengo un TableLayoutPanel con 3 columnas y 1 fila: (botón, control de usuario eliminar, añadir el botón)¿Cómo añadir filas en medio de una TableLayoutPanel

Quiero el botón Añadir para añadir una nueva fila similar a lo anterior debajo del botón de clics: por ejemplo: ANTES:

  1. (botón 1 botón 1, control de usuario 2 Eliminar, Añadir)
  2. (botón 2, control de usuario 2 Eliminar, Añadir botón 2)

Después de hacer clic en "Añadir botón 1":

  1. (botón 1, Control de Usuario 2 Retire, el botón Agregar 1)
  2. (botón 3, Control de Usuario 3 Retire, el botón Agregar 3)
  3. (Quitar el botón 2, Control de usuario 2, botón Agregar 2)

Logré agregar la fila al final del panel de la mesa pero no al medio: sigue atornillando el diseño. He aquí un fragmento del controlador de eventos:

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

esto no funciona correctamente.

¿Estoy haciendo algo mal? ¿alguna sugerencia?

Respuesta

6

encontrado finalmente una solución: En lugar de añadir los controles a emabrgo ubicación directa, los estoy añadiendo a la final y luego utilizar la función SetChildIndex() para mover el control a la posición deseada:

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

+1 ¡Muchas gracias por compartir, Eldad! Tuve exactamente el mismo problema. Simplemente no tenía sentido por qué agregar elementos al principio y al final de la colección funcionó, pero falló en cualquier punto entre el inicio y el final ... SetChildIndex no solo eliminó muchos códigos, sino que también funciona como un encanto :) ¡thx otra vez! – libjup

+0

Me alegro de haber podido ayudar :) – Eldad

Cuestiones relacionadas