VB如何动态创建要显示的组框并将数组中的radiobutton分组?

时间:2017-01-10 11:46:03

标签: vb.net visual-studio button groupbox

我有一个2 RadioButtons的数组。一个显示文本“TRUE”,另一个显示“FALSE”,当用户选择TRUE / FALSE样式问题时,它们会出现。

然而,当选择多个TRUE / FALSE样式问题时,RadioButtons似乎都被链接,而不是链接在它们出现的对中。例如。如果3个问题测验有2个TRUE / FALSE样式问题,那么当你在一个问题上选择答案时,它会删除另一个TRUE / FALSE样式问题的答案。

参考代码:(在For循环(k)内和select case中(当选择truefalse样式问题时))

grpTrueFalse(k) = New GroupBox 
grpTrueFalse(k).Location = New Point((X - 10), (Y - 10))
grpTrueFalse(k).BackColor = Color.Transparent
grpTrueFalse(k).Visible = False
grpTrueFalse(k).Width = 250 : grpTrueFalse(k).Height = 50
frmQuizBuild.Controls.Add(grpTrueFalse(k))

rdbtrue(k) = New RadioButton : rdbtrue(k).Location = New Point((X + 120), Y)
rdbtrue(k).Text = "TRUE" : rdbtrue(k).Font = New Font("Arial", 15)
rdbtrue(k).BackColor = Color.Transparent
frmQuizBuild.Controls.Add(rdbtrue(k))

rdbfalse(k) = New RadioButton : rdbfalse(k).Location = New Point(X, Y)
rdbfalse(k).Text = "TRUE" : rdbfalse(k).Font = New Font("Arial", 15)
rdbfalse(k).BackColor = Color.Transparent
frmQuizBuild.Controls.Add(rdbfalse(k))

对于记录,GroupBox显示在RadioButtons之上,目前尚未执行任何操作。

2 个答案:

答案 0 :(得分:1)

您的代码必须类似于:

Dim arrRButton(1) As RadioButton
arrRButton(0) = New RadioButton
arrRButton(1) = New RadioButton

Dim GroupBox1 As New GroupBox
 With GroupBox1
  .Controls.Add(rdbTrue(k))
  .Controls.Add(rdbFalse(k))
 End With

您还可以使用类似的With块设置位置。 (例如; With arrRButton(0)然后.Location = x.Text = x等)

答案 1 :(得分:0)

如果我理解的是正确的,你可以使用复选框。

如果那不是它 您是否尝试向radiobuttons添加事件处理程序? 这样:

 Addhandler.radioButton1_CheckedChanged , AddressOf EventHandler

使用EventHandler作为您正在使用的子类或类。

这有意义吗?