2011-11-01 32 views
5

Ho un TableLayoutPanel con 3 colonne e 1 riga: (pulsante, di controllo utente rimuovere, pulsante Aggiungi)Come aggiungere righe in mezzo ad un TableLayoutPanel

voglio il pulsante Add per aggiungere una nuova riga simile a il sopra sotto il pulsante cliccato: esempio: PRIMA:

  1. (tasto 1, User Control 2 Togliere, aggiungere il pulsante 1)
  2. (tasto 2, User Control 2 Togliere, aggiungere il pulsante 2)

Dopo aver cliccato su "Aggiungi pulsante 1":

  1. (tasto 1, User Control 2 Togliere, aggiungere il pulsante 1)
  2. (tasto 3, di controllo utente 3 Togliere, aggiungere il pulsante 3)
  3. (Rimuovi pulsante 2, Controllo utente 2, Aggiungi pulsante 2)

Sono riuscito ad aggiungere la riga alla fine del tablelayoupanel ma non al centro: Continua a rovinare il layout. Ecco un frammento del gestore di eventi:

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

questo non funziona correttamente.

Sto facendo qualcosa di sbagliato? eventuali suggerimenti?

risposta

6

infine trovato una soluzione: Invece di aggiungere i controlli per THIER posizione diretta, Li sto aggiungendo alla fine e quindi utilizzare la funzione SetChildIndex() spostare il controllo nella posizione desiderata:

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 Grazie mille per aver condiviso, Eldad! Ho avuto ESATTAMENTE lo stesso problema. Non aveva proprio senso il motivo per cui aggiungere elementi all'inizio e alla fine della collezione funzionava, ma non riusciva da nessuna parte tra l'inizio e la fine ... SetChildIndex non solo ha tolto molto codice ma funziona anche come un fascino :) grazie ancora! – libjup

+0

Felice di poter aiutare :) – Eldad

Problemi correlati