Eld*_*dad 5 user-controls tablelayoutpanel winforms
我有一个包含3列和1行的TableLayoutPanel :(删除按钮,用户控件,添加按钮)
我想要添加按钮添加一个类似于上面单击按钮的新行:例如:BEFORE:
点击"添加按钮1"后:
我设法将行添加到tablelayoupanel的末尾,但不添加到中间:它一直搞砸了布局.这是事件处理程序的片段:
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);
}
Run Code Online (Sandbox Code Playgroud)
这不能正常工作.
难道我做错了什么?有什么建议?
最后找到了一个解决方案:不是将控件添加到直接位置,而是将它们添加到最后,然后使用该SetChildIndex()函数将控件移动到所需位置:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3253 次 |
| 最近记录: |