Winform - Groupbox & Controls?

In Winform, I have a group package and it has several Textbox controls. If I delete Groupbox, the text fields will also be deleted. They are somehow attached to Groupbox, although I did not do anything conscious to make this happen. Question. How to delete this connection to remove the group box and still have text fields in the form?

+3
source share
3 answers

Child controls have the Parent property. If you remove the parent, Windows Forms will automatically call Dispose () for the children. This is one of the reasons you will never have to explicitly call Dispose () on your child controls when you close the form.

, , , this.Controls.Add(). WF , . :

    private void button1_Click(object sender, EventArgs e) {
        int nextTab = 0;
        foreach (Control ctl in this.Controls) nextTab = Math.Max(nextTab, ctl.TabIndex);
        Point offset = groupBox1.Location;
        for (int ix = groupBox1.Controls.Count - 1; ix >= 0; --ix) {
            Control ctl = groupBox1.Controls[ix];
            ctl.Location = new Point(ctl.Left + offset.X, ctl.Top + offset.Y);
            ctl.TabIndex += ++nextTab;
            this.Controls.Add(ctl);
        }
        groupBox1.Dispose();
        groupBox1 = null;
    }
+5

Designer.cs , :

this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);

, :

this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);

, , . , Position , Groupbox.

+5

.

Another option is to manually edit the file created by the designer (Foo.Designer.cs) and delete the group package there. The participant’s declaration is at the very end of the constructor file, and all configuration work is performed in InitializeComponent(). If you first delete the member declaration, compiler errors should point to places where you still need to delete some lines. This works, but, as always, be careful when editing manually generated files :-).

+4
source

Source: https://habr.com/ru/post/1723625/


All Articles