gpt4 book ai didi

winforms - 如何将行添加到 TableLayoutPanel 的中间

转载 作者:行者123 更新时间:2023-12-04 18:49:50 27 4
gpt4 key购买 nike

我有一个包含 3 列和 1 行的 TableLayoutPanel:
(删除按钮、用户控制、添加按钮)

我希望添加按钮在单击的按钮下方添加一个类似于上面的新行:
例如:
前:

  • (删除按钮 1、用户控件 2、添加按钮 1)
  • (删除按钮 2、用户控件 2、添加按钮 2)

  • 单击“添加按钮 1”后:
  • (删除按钮 1、用户控件 2、添加按钮 1)
  • (删除按钮 3、用户控件 3、添加按钮 3)
  • (删除按钮 2、用户控件 2、添加按钮 2)

  • 我设法将行添加到 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);
    }

    这不能正常工作。

    难道我做错了什么?有什么建议?

    最佳答案

    终于找到了一个解决方案:我没有将控件添加到他们的直接位置,而是将它们添加到末尾,然后使用 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);
    }

    关于winforms - 如何将行添加到 TableLayoutPanel 的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7964239/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com