TheBlinderCoder
TheBlinderCoder

Reputation: 39

Select text based on which radio button is checked

I've got a piece of code and I am using 5 radio buttons. I have 5 IF statements each for one radio button. But only one can be active at a time.

How can I write it without all the if statements? The code is used to update the textbox with the results from the currency calculator

If RadioAmerica.Checked Then
    Resultbox.AppendText(CurrencyHolder.Text & " Amerikaanse dollars komen overeen met:")
    thevalue = usdvalue
End If

If RadioBritish.Checked Then
    Resultbox.AppendText(CurrencyHolder.Text & " Britseponden komen overeen met:")
    thevalue = gbpvalue
End If

If RadioEuro.Checked Then
    Resultbox.AppendText(CurrencyHolder.Text & " Euro's komen overeen met:")
    thevalue = eurovalue
End If

If RadioRussian.Checked Then
    Resultbox.AppendText(CurrencyHolder.Text & " Russische roebels komen overeen met:")
    thevalue = rusvalue
End If

If RadioJapan.Checked Then
    Resultbox.AppendText(CurrencyHolder.Text & " Japanese Yens komen overeen met:")
    thevalue = japyenvalue
End If

Upvotes: 0

Views: 2382

Answers (3)

Alaa Sadik
Alaa Sadik

Reputation: 1089

Here is the simplest way to do that

Select Case True
  Case RadioButton1.Checked
       ' code here
        Resultbox.AppendText(CurrencyHolder.Text & " Amerikaanse dollars komen overeen met:")
        thevalue = usdvalue
  Case RadioButton2.Checked
       ' code here
  Case RadioButton3.Checked
       ' code here
  Case RadioButton4.Checked
       ' code here
  Case RadioButton5.Checked
       ' code here
  Case Else
End Select

Upvotes: 1

rheitzman
rheitzman

Reputation: 2297

I prefer a case statement over a series of IF .. Then or If .... ElseIf ....Then ... statements.

In the example the CheckChanged evented for all the buttons was hooked to one Sub:

Private Sub RadioButtonX_CheckedChanged(sender As Object, e As EventArgs) _ 
Handles RadioButton5.CheckedChanged, RadioButton4.CheckedChanged, 
RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, 
RadioButton1.CheckedChanged
    Select Case sender.name
        Case "RadioButton1"
            Label1.Text = sender.name
        Case "RadioButton2"
            Label1.Text = sender.name
        Case "RadioButton3"
            Label1.Text = sender.name
        Case "RadioButton4"
            Label1.Text = sender.name
        Case "RadioButton5"
            Label1.Text = sender.name
    End Select
End Sub

To quickly hook them together select all the controls then add the CheckChanged() event via properties.

If you placed the proper value in the .Tag properties you could use the same sub:

   theValue = Sender.Tag 

Note that you can use Data Binding with the Tag property.

Upvotes: 3

Cody Gray
Cody Gray

Reputation: 244772

Radio buttons that represent a "group" of items should be grouped together in a container. The GroupBox control is expressly designed for this purpose. If all of your currency radio buttons are not already in a GroupBox, put them there.

Then, you can just iterate through the radio buttons in the GroupBox and see which one is checked. This can even be simplified with LINQ:

Dim rb As RadioButton = myGroupBox.Controls.OfType(Of RadioButton)
                           .FirstOrDefault(Function(r) r.Checked = True)

But in order for this to work in your case, you have to associate the relevant data with each radio button. You could use the Tag property to do that. Add those strings to each radio button's Tag property, and then you can do:

Dim rb As RadioButton = myGroupBox.Controls.OfType(Of RadioButton)
                           .FirstOrDefault(Function(r) r.Checked = True)

Resultbox.AppendText(CurrencyHolder.Text & rb.Tag.ToString())

Unfortunately, it doesn't scale. If you have multiple pieces of data that need to be associated with each radio button, you will have to work a lot harder. You can either:

  1. Create a custom radio button class that derives from the built-in RadioButton class. Inside of your custom class, provide properties that expose the information you need. Then, use the code above, but cast the result to an object of your custom radio button class type, and use the properties it provides.

  2. Keep things simple and just use If statements. No harm in that, it is the idiomatic way of making decisions in code and can still be very readable.

Upvotes: 2

Related Questions