Newbie Radiobutton Question

Hi,

After much hunting I can't seem to find out how to do this.

I want to check the state of 5 radio buttons. Here's what i want in pseudo code:



for each xRadio in Group_of_RadioButtons
   If radiobutton(x).checked = false then
      msgbox("Some Message")
   Else
      radiobutton(x).value = "some text"
   end if
Next

 


So my questions are these:

1. How do you iterate through a group of radiobuttons (for each Radio in GroupBox.Controls doesn't work - not a control)

2. How do you assign an arbitrary string value to the radiobutton So if RadioButton1 is checked the string returned is a Tab (for example)

I assume 2 relates to setting up some new kind of property for radiobuttons but i can't seem to work this out.

I realise these are probably very simple questions, but my books and google seem to have failed me on this occasion!

Any help, much appreciated!

James



Answer this question

Newbie Radiobutton Question

  • JosepMiquel

    Something like the following.  Will Change the text on the radio buttons.  When you click on a button it will chnage the text or display messagebox as well as update the Label1 with a value from the Radio button tag property of the checked radio button.

    I think the tag property of each of the radio buttons will enable you to return and arbitary string from a radio button.   That is not the radio button text property.



    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            SetRadioText("RadioButton Form Load")
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Obj As RadioButton
            Dim IIndex As Integer = 0

            For Each Obj In Me.GroupBox1.Controls
                If Obj.Checked = True Then
                    IIndex = SplitName(Obj.Name)
                    Select Case IIndex
                        Case 1
                            MessageBox.Show("Item1 Selected" & vbCrLf & "leave Radio Button Captions")
                        Case 2
                            SetRadioText("Option 2 text")
                        Case 3
                            SetRadioText("Option3 Text")
                        Case Else
                            SetRadioText("Default Radio Button")
                    End Select

                    '//Return a specific value from selected Radio Button
                    Me.Label1.Text = Obj.Tag.ToString


                    Exit For
                End If
            Next
        End Sub
        Sub SetRadioText(ByVal TextString As String)
            Dim obj As RadioButton
            Dim iindex As Integer

            For Each obj In Me.GroupBox1.Controls
                iindex = SplitName(obj.Name)
                obj.Text = TextString & " " & CType(iindex, String)
            Next

        End Sub

        Private Function SplitName(ByVal Name As String) As Integer
            '//Splits the Index from the Name       
            Dim iIndex As Integer
            Dim StrIndex As String = ""

            For iIndex = 0 To Name.Length - 1
                If IsNumeric(Name.Substring(iIndex, 1)) Then
                    StrIndex = StrIndex & Name.Substring(iIndex, 1)
                End If
            Next

            Return CType(StrIndex, Integer)
        End Function

     
    End Class



    If you dont want the button - just move the code to some different even such as

    Private Sub RadioButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click, RadioButton4.Click

    End Sub


  • Abobrix

    Hi,

    To make it simple:

    Question #1:
    The traversing GroupBox1.Controls would work. All you have to do is check the type if its a radiobutton if it is then cast it in your temporary radiobutton variable.

    Question #2:
    You could use the Tag property of the radiobutton to place string values in a radiobutton...

     

    cheers,

    Paul June A. Domag



  • Kathy0505

    Thanks both for your help.

    I think the problem that was confusing me was casting the radiobutton into  controls that you could iterate through in a collection.

    Wish I'd spotted that Tag property sooner, pretty obvious!

    Anyway, on the back of this, I've found an example on MSDN for anyone else who might be interested in this.

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnadvnet/html/vbnet05132003.asp

    Thanks again, this has been holding me up for days!

    James

  • Newbie Radiobutton Question