如何将动态创建的项添加到动态创建的Panel C#WinForm中

时间:2015-02-23 14:22:07

标签: c# dynamic panel

我有一个列表组合框,当进行选择时会触发创建需要填写的文本框和标签。但是由于格式化并且如果选择未选中,我认为使用动态创建的面板会更容易,然后将这些框/标签添加到停靠面板。这样,如果未选中该框,我可以删除整个面板,其他面板将向上滑动,而不会在页面上留下任何空白空间。

我有代码来创建面板,我有代码来创建标签和文本框。但是我很难理解如何使用创建面板并将框添加到面板。

以下是我目前用于创建各种部件的代码。

public TextBox AddNewTextBox()
    {
        TextBox txt = new TextBox();
        panel_Request_Specific_Data.Controls.Add(txt);
        txt.Top = A * 27;
        if (ControlAcross == 0)
        {
            txt.Left = 90;
            ControlAcross = 1;
            txt.BringToFront();
        }
        else if (ControlAcross == 1)
        {
            txt.Left = 330;
            ControlAcross = 2;
            txt.BringToFront();
        }
        else if (ControlAcross == 2)
        {
            txt.Left = 570;
            ControlAcross = 3;
            txt.BringToFront();
        }
        else if (ControlAcross == 3)
        {
            txt.Left = 810;
            ControlAcross = 0;
            txt.BringToFront();
            A = A + 1;
        }
        txt.Text = "TextBox" + this.A.ToString() + "." + ControlAcross.ToString();

        return txt;
    }
    public Label AddNewLabel()
    {
        Label lbl = new Label();
        panel_Request_Specific_Data.Controls.Add(lbl);
        lbl.Top = A * 28;
        if (ControlAcross == 0)
        {
            lbl.Left = 10;
        }
        else if (ControlAcross == 1)
        {
            lbl.Left = 250;

        }
        else if (ControlAcross == 2)
        {
            lbl.Left = 490;

        }
        else if (ControlAcross == 3)
        {
            lbl.Left = 730;

        }
        lbl.Text = "Label" + this.A.ToString() + "." + ControlAcross.ToString();

        return lbl;
    }

    public Panel AddPanel(string PanelType)
    {

        Panel pnl = new Panel();
        panel_Request_Specific_Data.Controls.Add(pnl);
        pnl.Name = "pnl" + PanelType;
        pnl.Dock = DockStyle.Top;
        return pnl;

    }

0 个答案:

没有答案