0

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:

    Dim sqlda As New SqlDataAdapter("SELECT * FROM table1 WHERE column1= '" & lblWONo.Text & "'", Constr)
    Dim sqlds As New DataSet

    sqlds.Clear()
    sqlda.Fill(sqlds)

    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:

            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!

1 Answer 1

1

Think about the use of a dictionary (id, control) to store your controls. Then iterate the dictionary and set your state.

Sign up to request clarification or add additional context in comments.

3 Comments

could you provide an example??
On way is to store it in a kind of triple: (pseudo code) { .Item(2), rdbtn1No, rdbtn1Yes.Checked; .Item(3), rdbtn2No, rdbtn2Yes.Checked; ... } (feel free to find a collection which suites best for you) then write a loop which iterates the collection. By the way a would kill the if-statements: opt3Y.Checked = .Item(4) and opt3N.Checked = Not opt3Y.Checked.
@ReignOfRayn25: Would be nice if you can give some feedback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.