Attach components to GroupBox in C#

浪子不回头ぞ 提交于 2019-12-05 17:56:05

问题


I want to insert a group box in the form and put in 3 radio buttons in it.

Are there any advantages in attaching the 3 radio buttons to the groupbox.? Cab we even do that?

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?


回答1:


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:

RadioButton rb = new RadioButton();
rb.Text = "Some text";
myGroupBox.Controls.Add(rb);
rb.Location = new Point(someX, someY);

// repeat as necessary



回答2:


In code, assuming that you have a groupbox variable name 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.




回答3:


Also you can do it on one line:

groupBox1.Controls.AddRange(new Control[] { radioButton1, radioButton2, radioButton3 });


来源:https://stackoverflow.com/questions/1410884/attach-components-to-groupbox-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!