0

我有一个包含 12 个组框的表单,每个组框都有 3 个单选按钮。我for loop用来获取选中的单选按钮并获取单选按钮的值。我声明字符串和整数变量通过组框和单选按钮。我的问题是如何连接字符串变量和整数变量。示例代码:

 Dim opt, opt1, opt2, opt3, opt4, opt5, opt6, opt7, opt8, opt9, opt10, opt11, opt12 As String
    Dim grpboxcnt As Integer = 1
    Dim rdbtncnt As Integer = 1
    Dim xrdbtn As String

    For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()

        For Each rdbtn As RadioButton In Me.Controls("GroupBox" & grpboxcnt).Controls.OfType(Of RadioButton)()
            If rdbtn.Checked = True Then
                If rdbtn.Text = "Yes" Then
                    opt & rdbtncnt = 1 '(i want to be like this way but it is an error and it is not the proper way, is there any way to concat string variable to integer variable?) if this is checked i want to concat the string(opt) and integer(rdbtncnt) example opt & rdbtncnt = 1 ;but it gives me error 
                ElseIf rdbtn.Text = "No" Then
                    opt & rdbtncnt = 0 '(i want to be like that but it is an error and it is not the proper way, is there any way to concat string variable to integer variable?) if this is checked then i want to concat the string(opt) and integer(rdbtncnt) example opt & rdbtncnt = 0 ;but it gives me error
                ElseIf rdbtn.Text = "NA" Then
                    opt & rdbtncnt  = 2 '(i want to be like that but it is an error and it is not the proper way, is there any way to concat string variable to integer variable?) if this is checked i want to concat the string(opt) and integer(rdbtncnt) example opt & rdbtncnt = 2 ;but it gives me error
                End If
            End If
            Next
         rdbtncnt += 1 'then rdbtncnt increment new set of radiobuttons will be looped
        grpboxcnt += 1 'then grpboxcnt increment new name of groupbox will be looped
    Next

sqlcommand.commandType = "UPDATE table1 SET radio1 = @opt1, radio2 = @opt2 , radio2 = @opt3 , etc... WHERE tableID = 1"

提前致谢!

4

3 回答 3

2

我想我理解你想要做什么 - 听起来你想opt根据你所处的位置使用正确的变量RadioButton- 即,如果你在RadioButton1,你想使用opt1,如果你在RadioButton2你想要使用opt2

不幸的是,你不能通过连接两个不同的变量来引用一个变量来形成它的名字——至少,我不知道。

有几种方法可以解决这个问题 - 您可以使用 aList(Of Integer)以与 s 相同的顺序保存值RadiButton,或者您可以使用 aDictionary(Of String, Integer)将名称保存为键(“opt”和 RadionButton 编号)和您选择的值。

由于您似乎打算使用它来为 SQL 命令提供参数值,因此我推荐使用字典,因为您可以通过将参数名称作为键传递来获得正确的值:

Dim radioButtonValues As New Dictionary(Of String, Integer)
Dim grpboxcnt As Integer = 1
Dim rdbtncnt As Integer = 1

For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()

    For Each rdbtn As RadioButton In Me.Controls("GroupBox" & grpboxcnt).Controls.OfType(Of RadioButton)()
        If rdbtn.Checked = True Then
            If rdbtn.Text = "Yes" Then
                radioButtonValues.Add("@opt" & rdbtncnt.ToString(), 1) 
            ElseIf rdbtn.Text = "No" Then
                radioButtonValues.Add("@opt" & rdbtncnt.ToString(), 0)
            ElseIf rdbtn.Text = "NA" Then
                radioButtonValues.Add("@opt" & rdbtncnt.ToString(), 2)
            End If
        End If
     Next
     rdbtncnt += 1
     grpboxcnt += 1
Next

这将创建一个字典,其中“@opt1”作为第一个的键,“@opt2”作为第二RadioButtonList个的键,依此类推RadioButtonList。我在键中添加了“@”,因为您将在以下代码中使用它作为参数名称:

' Note that it should be CommandText, not CommandType
sqlcommand.CommandText = "UPDATE table1 SET radio1 = @opt1, radio2 = @opt2 , radio2 = @opt3 , etc... WHERE tableID = 1"
sqlcommand.CommandType = CommandType.Text

For Each key As String in radioButtonValues.Keys

    sqlcommand.Parameters.AddWithValue(key, radioButtonValues(key))
Next

本质上,对于字典中的每个键值对,您将键作为参数名称添加,并将它的值作为参数值添加到 SQL 命令的参数集合中。

笔记

这有点脆弱,因为您必须确保 SQL 语句具有正确数量的参数并且它们的名称正确。您可以扩展上面的代码以根据字典中的键创建 SQL 命令,然后添加参数,但您最终会通过字典两次,而不是一次(一次构建 SQL,第二次构建参数集合)。

此外,您在外循环中递增rdbtncntgrpboxcnt我不确定这是否是您想要的(除非您知道RadioButton每个中只有一个GroupBox)。

于 2013-08-14T08:16:10.300 回答
0
 Dim opt, opt1, opt2, opt3, opt4, opt5, opt6, opt7, opt8, opt9, opt10, opt11, opt12 As String
    Dim grpboxcnt As Integer = 1
    Dim rdbtncnt As Integer = 1
    Dim xrdbtn As String
    Dim result As String
    For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()

        For Each rdbtn As RadioButton In Me.Controls("GroupBox" & grpboxcnt).Controls.OfType(Of RadioButton)()
            If rdbtn.Checked = True Then
                If rdbtn.Text = "Yes" Then
                    opt1 = 1 

                ElseIf rdbtn.Text = "No" Then
                    opt1 = 0 
                ElseIf rdbtn.Text = "NA" Then
                    opt1 = 2 
                End If
                rdbtncnt2Str = rdbtncnt.ToString()
                result = opt1 & rdbtncnt2str
            End If
            Next
         Integer.TryParse(result, rdbtncnt)
         rdbtncnt += 1 
        grpboxcnt += 1 
    Next

sqlcommand.commandType = "UPDATE table1 SET radio1 = @opt1, radio2 = @opt2 , radio2 = @opt3 , etc... WHERE tableID = 1"

希望这可以帮助..

于 2013-08-14T04:51:21.317 回答
0

你可以试试:

rdbtncnt2Str = rdbtncnt.ToString()

或者

rdbtncnt2Str = Convert.ToString(rdbtncnt)

这会将整数转换为字符串,然后您可以使用以下方法进行连接:

暗淡结果为字符串

结果 = 选择 & rdbtncnt

于 2013-08-14T03:56:48.833 回答