gpt4 book ai didi

winforms - Winform - 分组框和控件?

转载 作者:行者123 更新时间:2023-12-03 05:04:47 28 4
gpt4 key购买 nike

在 Winform 中,我有一个组框,其中有几个文本框控件。如果我删除组框,文本框也会被删除。它们以某种方式与 Groupbox 联系在一起,尽管我没有故意做任何事情来实现这种情况。问题 - 如何删除此连接,以便我可以删除组框并在表单上保留文本框?

最佳答案

子控件有一个 Parent 属性。如果删除其父级,Windows 窗体也会自动对子级调用 Dispose()。这就是关闭窗体时您不必在子控件上显式调用 Dispose() 的原因之一。

获得你想要的东西很容易,使用 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;
}

关于winforms - Winform - 分组框和控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1780466/

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