在 C# 中将组件附加到 GroupBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1410884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attach components to GroupBox in C#
提问by zack
I want to insert a group box in the form and put in 3 radio buttons in it.
我想在表单中插入一个组框并在其中放入 3 个单选按钮。
Are there any advantages in attaching the 3 radio buttons to the groupbox.? Cab we even do that?
将 3 个单选按钮附加到 groupbox 是否有任何优势。?出租车我们甚至这样做?
If I have to do it how do i attach the 3 radio buttons to the groupbox so that they become part of the group box and not separate components on the form?
如果我必须这样做,我如何将 3 个单选按钮附加到 groupbox 以便它们成为 group box 的一部分而不是表单上的单独组件?
采纳答案by Fredrik M?rk
If you are talking winforms; simply drag the radio button controls into the GroupBox
in the forms designer. If you want to add them programmatically, something like this should work:
如果你说的是winforms;只需将单选按钮控件拖到GroupBox
表单设计器中。如果你想以编程方式添加它们,这样的事情应该可以工作:
RadioButton rb = new RadioButton();
rb.Text = "Some text";
myGroupBox.Controls.Add(rb);
rb.Location = new Point(someX, someY);
// repeat as necessary
回答by Alistair Evans
In code, assuming that you have a groupbox variable name groupBox1:
在代码中,假设您有一个 groupbox 变量名称 groupBox1:
groupBox1.Controls.Add(radioButton1);
groupBox1.Controls.Add(radioButton2);
groupBox1.Controls.Add(radioButton3);
If you mean in terms of the designer, just drag the radiobuttons on to the groupbox rather than the form.
如果您的意思是在设计器方面,只需将单选按钮拖到 groupbox 而不是表单上。
回答by Cobra91151
Also you can do it on one line:
您也可以在一行上完成:
groupBox1.Controls.AddRange(new Control[] { radioButton1, radioButton2, radioButton3 });