0

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

我正在计算并显示一个人需要多少卡路里来维持当前体重。我有一22 radioButtons:一组是针对性别的,男性或女性,另一组是针对他们的活动率,活跃或不活跃。

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

4

2 回答 2

0

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

于 2013-03-17T16:50:49.223 回答
0

您应该使用 GroupBoxes 将一组单选按钮放在一个组框内,另一组放在另一个组框内

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

以下代码只是手动构建表单的示例,与设计器一起工作将在 InitializeComponent 方法中为您创建与此代码等效的代码。请注意这两个单选按钮集是不同容器(组框)的子项

' 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()
于 2013-03-17T16:51:01.173 回答