1

I have a group with a NumberUpDown control, 0-10 by 1 and 11 radio buttons (equal to the range of the NumberUpDown control). I have been able to write code in the events of the controls such that when a radio button is checked the value of the NumberUpDown changes to that of the radio button checked and when the NumberUpDown value is changed, the correct radio button is checked.

There are users that want to use a stylus to click on the radio buttons and there are those that want to use the keyboard to enter the values.

Below is the code I wrote. The LONG if/then/else should be able to be written a different way. It would seem that RadioButtonArray (VB6) would solve my problem, but that's old school. I'm just not familiar with VB.NET enough to figure out how. I'm using Visual Studio 2010.

PainAcceptable is the NumberUpDown control. PainAx are the individual radio buttons.

Private Sub PainAcceptable_ValueChanged(sender As System.Object, e As System.EventArgs) Handles PainAcceptable.ValueChanged
    If PainAcceptable.Value = 0 Then
        PainA0.Checked = True
    ElseIf PainAcceptable.Value = 1 Then
        PainA1.Checked = True
    ElseIf PainAcceptable.Value = 2 Then
        PainA2.Checked = True
    ElseIf PainAcceptable.Value = 3 Then
        PainA3.Checked = True
    ElseIf PainAcceptable.Value = 4 Then
        PainA4.Checked = True
    ElseIf PainAcceptable.Value = 5 Then
        PainA5.Checked = True
    ElseIf PainAcceptable.Value = 6 Then
        PainA6.Checked = True
    ElseIf PainAcceptable.Value = 7 Then
        PainA7.Checked = True
    ElseIf PainAcceptable.Value = 8 Then
        PainA8.Checked = True
    ElseIf PainAcceptable.Value = 9 Then
        PainA9.Checked = True
    ElseIf PainAcceptable.Value = 10 Then
        PainA10.Checked = True
    End If
End Sub
' When radio button PainA1 is changed, PainAcceptable is set to the value 1
Private Sub PainA1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles PainA1.CheckedChanged
    If PainA1.Checked Then
        PainAcceptable.Value = 1
    End If
End Sub
4

2 回答 2

2

假设我的收音机被命名rbPain##,其中## 是 A0、A1 ... A9。我的收音机是基础的。由于收音机需要一个组控件才能作为一个组,因此我在组框或面板上拥有全部 10 个(0 - 9)。如果您不喜欢边框样式等的外观。最后,面板上没有其他控件,但我的rbPains

Dim rbNameToFind as string = "rbPain"

rbNameToFind & = PainAcceptable.Value.ToString
' rbNameToFind now matches my rb names... "rbPain00" ... "rbPain09"

panel.Controls(rbNametoFind).Checked = True

如果您确实必须在面板上有一些其他控件,请遍历它们以找到每个 rbPain 和一个名为您要查找的内容的控件:

Dim rb as Radiobutton

For each ctl as Control in painPnl.Controls
  if ctl.GetType is GetType(radiobutton) then

       ' found an rb!
       if ctl.Name =  rbNameToFind Then
           rb = Ctype(ctl, gettype(radiobutton))
           ' found ours!
            rb.Checked = true
            exit for                   ' hurry home
       end if
   end if
 next

编辑

您还可以简化 checkchanged 处理程序...而不是其中的 10 个:

 Private Sub PainA0_CheckedChanged(sender As System.Object, e As System.EventArgs) _ 
      Handles PainA1.CheckedChanged, PainA2.CheckedChanged, PainA3.CheckedChanged, _
              PainA4.CheckedChanged ... (you get the idea)

      ' I would not parse the name to get the value, but you could stash the 
      ' related value in .Tag where PainA0.Tag=1 or whatever.  1 line of code for
      ' all 10 RBs:

      PainAcceptable.Value = sender.Tag

      ' *** OR you could just move all the existing code to one sub basically:

      Dim Pain As Integer = 0
      Select Case sender.Name
          Case "PainA1"
              Pain = 1
          Case "PainA2"
              Pain = 2
          ...etc
      End Select
      PainAcceptable.Value = Pain

 End Sub

您可能遇到的另一个问题是 2 个控件更改一个值。当他们更改 NumericUpDn 时,您设置了 Radio 的 checkstate,这会导致 CheckChanged 事件,您在其中设置 NumericUpDn 值,您设置 checkstate 会导致....

在这种情况下,它只会迭代一次,因为一旦你设置了 Value 或 CheckState,它们实际上并没有第二次改变(对于 NUD 来说是这样,不确定 RB,但是当 NUD 没有实际改变时它会停止)。

最后,10 个 RB 是很多 RB。如果您使用 ComboBox 代替,您可以节省大量时间和代码。 cboPain.SelectedIndex会告诉你你想要什么没有任何 IF/Else 或 Select Case....

高温高压

于 2013-09-22T18:52:02.433 回答
0

尝试Controls.Find用于获取所需的单选按钮:
Find control by name from Windows Forms controls

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find%28VS.80%29.aspx

于 2013-09-22T17:22:58.830 回答