首页 > 解决方案 > 如何将复选框中的所有复选框添加到新表单?

问题描述

我想从中获取所有项目CheckedBoxList并将其添加到一个新项目中,Form因此对于每个已检查项目,我想要一个新Label的,带有 checkItem 名称和TextBox. 到目前为止,我正在这样做,但是当我打开表格时,我根本没有得到任何结果。我不知道如何获取检查的项目名称,我正在这样做:

labels[i].Text = i.ToString(); 

在此处输入图像描述

private void Button4_Click(object sender, EventArgs e)
    {
        testForm = new Test();
        TableLayoutPanel tableLayoutPanel = new TableLayoutPanel() { AutoSize = true };
        tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
        int n = 0;

        for (int i=0;i<checkedListBox1.CheckedIndices.Count;i++)
        {
            txtBox = new TextBox[checkedListBox1.CheckedIndices.Count];
            labels = new Label[checkedListBox1.CheckedIndices.Count];

            labels[i] = new Label();
            labels[i].Text = i.ToString();
            tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(0, n++));
            tableLayoutPanel.Controls.Add(labels[i]);

            txtBox[i] = new TextBox();
            tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            tableLayoutPanel.SetCellPosition(txtBox[i], new TableLayoutPanelCellPosition(0, n++));
            tableLayoutPanel.Controls.Add(txtBox[i]);

        }
        Controls.Add(tableLayoutPanel);
        testForm.ShowDialog();
    }

有什么建议么?谢谢你投入的时间。

标签: c#winforms

解决方案


解决方案是将代码更改为:

testForm.Controls.Add(tableLayoutPanel);

推荐阅读