如何在 vb.net 中的控件名称中连接变量整数 [英] How to concat variable integer in control name in vb.net

查看:24
本文介绍了如何在 vb.net 中的控件名称中连接变量整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个数据库并提取该数据并将其显示为表格,我有一个组框和单选按钮序列,在每个组框(groupbox1、groupbox2 等...)中有 2 个单选按钮,即 rdbtn1Yes 和 rdbtn1No(然后在下一个 Groupbox 中增加 +1).现在我使用 for 循环 来遍历每个组框和单选按钮.这是我的代码:

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

循环示例代码:

            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

仅此而已.先感谢您!

推荐答案

考虑使用字典(id、控件)来存储控件.然后迭代字典并设置你的状态.

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

这篇关于如何在 vb.net 中的控件名称中连接变量整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆