【问题标题】:How to concat variable integer in control name in vb.net如何在 vb.net 中的控件名称中连接变量整数
【发布时间】:2013-08-15 16:39:38
【问题描述】:

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

    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

仅此而已。先感谢您!

【问题讨论】:

    标签: vb.net winforms visual-studio-2012 for-loop user-controls


    【解决方案1】:

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

    【讨论】:

    • 你能举个例子吗??
    • 方法是将其存储在一种三元组中:(伪代码){ .Item(2), rdbtn1No, rdbtn1Yes.Checked; .Item(3), rdbtn2No, rdbtn2Yes.Checked; ... }(随意找到最适合您的集合)然后编写一个循环来迭代集合。顺便说一句,a 会杀死 if 语句:opt3Y.Checked = .Item(4) 和 opt3N.Checked = Not opt3Y.Checked。
    • @ReignOfRayn25:如果你能提供一些反馈就好了
    猜你喜欢
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    相关资源
    最近更新 更多