2011-11-01 45 views
5

Tôi có một TableLayoutPanel với 3 cột và 1 hàng: (Hủy bỏ nút, User Control, Thêm nút)Làm thế nào để thêm hàng vào giữa một TableLayoutPanel

Tôi muốn vào nút Add để thêm một hàng mới tương tự trên dưới nút nhấp: ví dụ: TRƯỚC:

  1. (nút 1, điều 2 Di chuyển, nút Add 1)
  2. (nút 2, điều 2 Di chuyển, Thêm nút 2)

Sau khi nhấp nút "1 Add":

  1. (nút 1, Điều 2 Di chuyển, nút Add 1)
  2. (nút 3, Điều 3 Di chuyển, Thêm nút 3)
  3. (Xóa nút 2, Điều khiển người dùng 2, nút Thêm 2)

Tôi đã quản lý để thêm hàng vào cuối tablelayoupanel nhưng không được thêm vào giữa: Nó tiếp tục làm xáo trộn bố cục. Đây là đoạn trích của trình xử lý sự kiện:

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

Điều này không hoạt động bình thường.

Tôi có làm gì sai không? bất kỳ đề xuất?

Trả lời

6

Cuối cùng tìm ra giải pháp: Thay vì thêm các điều khiển để Thier vị trí trực tiếp, tôi bổ sung chúng vào cuối và sau đó sử dụng chức năng SetChildIndex() để di chuyển điều khiển đến vị trí mong muốn:

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 Cảm ơn bạn rất nhiều vì đã chia sẻ, Eldad! Tôi đã có cùng vấn đề y hệt. Nó chỉ không làm cho bất kỳ ý nghĩa tại sao thêm các yếu tố ở đầu và cuối của bộ sưu tập đã làm việc nhưng không thành công bất cứ nơi nào giữa bắt đầu và kết thúc ... SetChildIndex không chỉ lấy đi nhiều mã mà còn hoạt động như một say mê :) thx một lần nữa! – libjup

+0

Vui vì tôi có thể giúp :) – Eldad

Các vấn đề liên quan