分别处理两套

时间:2013-03-17 16:47:56

标签: vb.net visual-studio-2010 radio-button

编程新手的困境......在我的程序中。

我正在计算和显示一个人维持当前体重所需的卡路里数。我有22 radioButtons:1套针对性别,男性或女性,另一套针对其活动率,有效或无效。

使用两组不同的radioButtons,每个都应该可以选择一个,但是一次只能选择一个单选按钮,如何告诉程序处理两组单选按钮分别?

2 个答案:

答案 0 :(得分:0)

单选按钮分组工作。用容器和你的好东西分开它们。

答案 1 :(得分:0)

你应该使用GroupBoxes将一组radiobuttons放在一个groupbox中,另一组放在另一个Group框内

通过这种方式,每组radiobutton都与另一组相隔离并按预期工作

以下代码只是手动表单构建的一个示例,与设计人员一起使用将在InitializeComponent方法中为您创建此代码的等效代码。请注意两个radiobutton集是如何是不同容器(子组)的子节点

' A generic form
Dim f as Form = new Form()
f.Size = new Size(300, 500)

' Create a Group box
Dim b1 as GroupBox = new GroupBox()
b1.Text = "Gender"
b1.Location = new Point(0,0)

' Create two radiobutton for Gender
Dim r1 as RadioButton = new RadioButton()
r1.Text = "Male"
r1.Location = new Point(5,15)
Dim r2 As RadioButton = new RadioButton()
r2.Text = "Female"
r2.Location = new Point(5, 40)

' Add the two radiobuttons to the GroupBox control collection
b1.Controls.AddRange(new Control() {r1, r2})

' Repeat for the second set of radiobuttons
Dim b2 as GroupBox = new GroupBox()
b2.Text = "Activity Rate"
b2.Location = new Point(0,100)
Dim r3 As RadioButton = new RadioButton()
r3.Text = "Active"
r3.Location = new Point(5,15)
Dim r4 as RadioButton = new RadioButton()
r4.Text = "Inactive"
r4.Location = new Point(5,40)
b2.Controls.AddRange(new Control() {r3, r4})

' Finally add the GroupBoxes to the Form ControlsCollection
f.Controls.AddRange(new Control() {b1, b2})

f.ShowDialog()