Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Now I have a database and pull out that data and display it to form,i have a sequence of groupbox and radiobuttons, in each groupbox (groupbox1,groupbox2,etc...) there are 2 radio buttons namely rdbtn1Yes and rdbtn1No (then it increment +1 in next Groupbox). now i use for loop to go through every groupboxes and radio buttons. And this is my code:
VB
If sqlds.Tables(0).Rows.Count > 0 Then

        With sqlds.Tables(0).DefaultView.Item(0)

            txtDateCreated.Value = .Item(0).ToString
            txtComments.Text = .Item(1).ToString

            'check column if it contain FALSE/TRUE value
            'then toggle the radiobutton state to TRUE

            'In this part i know there is another/easiest way to checked radio buttons to TRUE value 
            'and this is my code using looping (below):

            If .Item(2) = False Then
                rdbtn1No.Checked = True
            Else
                rdbtn1Yes.Checked = True
            End If

            If .Item(3) = False Then
                rdbtn2No.Checked = True
            Else
                rdbtn2Yes.Checked = True
            End If

            If .Item(4) = False Then
                opt3N.Checked = True
            Else
                opt3Y.Checked = True
            End If
        End With
    End If

SAMPLE CODE FOR LOOPING:

VB
Dim itemNo As Integer
         Dim rdbtnSet As Integer = 1
         Dim grpboxCnt As Integer = 1

          For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()
             For itemNo = 2 To sqlds.Tables(0).Columns.Count

                 If .Item(itemNo) = True Then
                     rdbtn & rdbtnSet & "Yes".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
                 Else
                     rdbtn & rdbtnSet & "No".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
                 End If

             Next
              rdbtnSet += 1
              grpboxCnt += 1
          Next

Thats all. Thank you in advance!
Posted
Comments
ZurdoDev 15-Aug-13 21:17pm    
What's your question?
Uknownymous 15-Aug-13 21:35pm    
check the second code sample (SAMPLE CODE FOR LOOPING) the commented part.
ZurdoDev 15-Aug-13 21:41pm    
I don't quite follow exactly what each item is, but you could do something like rdbtn.Checked = rdbtnSet.checked = true to put them all in one line.
Uknownymous 19-Aug-13 4:08am    
itemNo is the column index of dataset...

Use "Controls" property of GroupBox to get its child controls (RadioButton in your case) by its name.

((RadioButton)grpbx.Controls["rdbtn1Yes"]).Checked = true
 
Share this answer
 
Use "Controls" property of GroupBox to get its child controls (RadioButton in your case) by its name.

((RadioButton)grpbx.Controls["rdbtn1Yes"]).Checked = true
 
Share this answer
 
Use "Controls" property of GroupBox to get its child controls (RadioButton in your case) by its name.

((RadioButton)grpbx.Controls["rdbtn1Yes"]).Checked = true
 
Share this answer
 
What you are looking for is an equivalent to control arrays.
Here are some good articles that talk about solutions that are not based on control arrays -
http://msdn.microsoft.com/en-us/library/aa289142%28v=vs.71%29.aspx[^]
The Closest thing to a VB6 control array in VB.NET[^]
 
Share this answer
 
I am not sure what you are exactly trying to do. But, here is my sample code and see if this helps and provide more details on what you need.

I would say name each option/radio button in the group box just to have a common char in the Name. So, it would be easy for you to identify while looping.


VB
Private Sub Groups_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Display(Me)
End Sub
Private Sub Display(ByVal parent As Control)
    For Each childControl As Control In parent.Controls
        Select Case True
            Case TypeOf (childControl) Is RadioButton
                If childControl.Name.Contains("1") Then
                    DirectCast(childControl, RadioButton).Checked = True
                Else
                    DirectCast(childControl, RadioButton).Checked = False
                End If
            Case TypeOf (childControl) Is GroupBox
                Display(childControl)
        End Select
    Next
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900