如何将行添加到 TableLayoutPanel 的中间
我有一个 3 列 1 行的 TableLayoutPanel: (“删除”按钮、“用户控件”、“添加”按钮)
我希望“添加”按钮在单击的按钮下方添加类似于上面的新行: 例如: 之前:(
- 删除按钮 1、用户控制 2、添加按钮 1)
- (删除按钮 2、用户控制 2、添加按钮 2)
单击“添加按钮 1”后:(
- 删除按钮 1、用户控制 2、添加按钮 1)
- (删除按钮 3、用户控件 3、添加按钮 3)
- (删除按钮 2、用户控件 2、添加按钮 2)
我设法将该行添加到 tablelayoutpanel 的末尾,但没有添加到中间:它不断搞乱布局。 这是事件处理程序的片段:
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 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:
- (Remove button 1, User Control 2, Add button 1)
- (Remove button 2, User Control 2, Add button 2)
After clicking"Add button 1":
- (Remove button 1, User Control 2, Add button 1)
- (Remove button 3, User Control 3, Add button 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于找到了解决方案:我没有将控件添加到直接位置,而是将它们添加到末尾,然后使用 SetChildIndex() 函数将控件移动到所需位置:
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: