Results 1 to 5 of 5

Thread: Code Generated Radio Button Issue

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    2

    Code Generated Radio Button Issue

    I am using VB.Net to create one radiobutton for each line in a text file.

    Code:
                For i = 0 To SFLines.Length - 1
                    SRadioButton = New RadioButton
                    With SRadioButton
                        .Text = SFInfo(i, 1) & ", " & SFInfo(i, 0)
                        .Location = New Point(10, ((i + 1) * 17))
                        .Size = New Size(180, 17)
                        .Name = "RadioButton" & i
                        Me.grpSRadio.Controls.Add(SRadioButton)
                    End With
                Next
    SFLines is the number of lines in the text files. SFInfo is an array that is created using a Split on each line. The code works fine and my group populates with all the radio buttons. The issue is with the .Checked of the radio buttons created. Because the radio buttons are created in the forms on load event I cannot add code that references the radio button. VB.Net states that the Name RadioButtonX is not defined.

    Here is what I tried to do:
    Code:
        Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
            Dim i
            Dim SRadioButton As New RadioButton
            For i = 0 To SFLines.Length - 1
                SRadioButton.Name = "RadioButton" & i
                If SRadioButton.Checked = True Then
                    MsgBox(SRadioButton.Text)
                End If
            Next
        End Sub
    This does not work.

    I need a Select button that will display some info about the array element based on the radio button selected.

    Any help would be greatly appreciated.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Code Generated Radio Button Issue

    Use this code:

    vb.net Code:
    1. Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
    2.     Dim i As Integer
    3.     Dim sRadioButton As RadioButton
    4.  
    5.     For i = 0 To SFLines.Length - 1
    6.         If TypeOf Me.grpSRadio.Controls("RadioButton" & i) Is RadioButton Then
    7.             sRadioButton = DirectCast(Me.grpSRadio.Controls("RadioButton" & i), RadioButton)
    8.             If sRadioButton.Checked Then
    9.                 MessageBox.Show(sRadioButton.Text)
    10.             End If
    11.         End If
    12.     Next
    13. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    2

    Re: Code Generated Radio Button Issue

    Thanks for the help, I'll have to try and see if that works. After playing with some code I found that is was easier to create a control array of radio buttons and evaluate the value of each element in the array so that if the element = -1 that is the selected radiobutton.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code Generated Radio Button Issue

    Assuming you're using .NET 3.5, here's the easiest way to find the checked RadioButton:
    vb.net Code:
    1. Dim checkedItem As RadioButton = Me.grpSRadio.Controls.OfType(Of RadioButton).Where(Function(rb As RadioButton) rb.Checked).First()
    That's LINQ function syntax that gets all RadioButtons from the Controls collection where the Checked property is True, then returns the first one of them.
    Last edited by jmcilhinney; Apr 8th, 2009 at 02:26 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code Generated Radio Button Issue

    Here's the LINQ query syntax equivalent, which some may find easier to understand as it doesn't use an explicit lambda expression:
    vb.net Code:
    1. Dim checkedItem As RadioButton = (From rb In Me.grpSRadio.Controls.OfType(Of RadioButton)() _
    2.                                   Where rb.Checked).First()
    Last edited by jmcilhinney; Apr 8th, 2009 at 02:26 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width