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
KeithSmith
  • 774
  • 2
  • 8
  • 26

2 Answers2

2

Assume MY radios are named rbPain## where ## is A0, A1 ... A9. My radios are zero based. Since radios sort of need a group control for them to act as a group, I have all 10 (0 - 9) on a groupbox or panel. If you dont like the look fiddle with borderstyle etc. Finally, there are no other controls on the panel but my rbPains

Dim rbNameToFind as string = "rbPain"

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

panel.Controls(rbNametoFind).Checked = True

If you do have to have some other controls on the panel iterate thru them all to find each rbPain and the one named what you are looking for:

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

EDIT

You can also simplify the checkchanged handler...instead of 10 of them:

 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

The other issue you might run into is 2 controls changing one value. When they change the NumericUpDn, you set the checkstate of a Radio which causes a CheckChanged event where you set the NumericUpDn value where you set the checkstate which causes....

In this case it will only iterate once because once you set the Value or CheckState they dont actually change the second time (that is true for the NUD, not sure of the RBs but it will stop when the NUD doesnt actually change).

Finally, 10 RBs is a lot of RBs. You could save yourself a lot of time and code if you were to use a ComboBox instead. cboPain.SelectedIndex would tell you what you want without any IF/Else or Select Case....

HTH

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • The above code works with the following modifications. The last end if should be Next. There needs to be another variable, rbCtl as RadioButton = ctl as ctl is a base control object and does not have Checked as a property. This is a lot of run-time code for something that should be as simple as rbPainCtrlArray[painAcceptable.value].Checked = True. Might make my own RadioButton array in Form.Load. – KeithSmith Sep 22 '13 at 21:14
  • Correct. Fixed the `End if` -> `Next`. I obviously did not use VS (case is all wrong). I also added `rb = Ctype(ctl, gettype(radiobutton)` to cast the ctl to a RB. See my Edit for a fewer Event handlers; for almost no code, use a ComboBOx instead of 10 RBs. But so far, we collapsed 20 lines to 5 or 6... Good luck – Ňɏssa Pøngjǣrdenlarp Sep 22 '13 at 21:22
0

Try to use Controls.Find for getting the desired radio button:
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

Community
  • 1
  • 1
haansn08
  • 157
  • 3
  • 10