首页 > 解决方案 > 如何将 SQL IN 语句与从树视图列表中选择的项目列表一起使用?

问题描述

如何将For Nextaccid循环(如下)中的字符串值连接到一个字符串中?

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim accid As String
        Dim iLast As Integer
        iLast = trv.Nodes.Count
        Dim p As Integer = 0
        For p = 0 To iLast - 1
            If TrV.Nodes(p).Checked = True Then
                accid = Strings.Left(TrV.Nodes(p).Text, 9)
                MsgBox(accid)
            End If
        Next
 End Sub

这给了我一个单独的字符串输出,"accid" 我想要这个输出:"accid1,accid2,accid3"

感谢支持!

标签: vb.net

解决方案


您需要在循环内构建字符串,然后在循环外执行 MsgBox。像这样的东西应该工作:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim accid As String
        Dim iLast As Integer
        iLast = trv.Nodes.Count
        Dim p As Integer = 0

        For p = 0 To iLast - 1
            If TrV.Nodes(p).Checked = True Then
                accid = accid & Strings.Left(TrV.Nodes(p).Text, 9) & ","  'notice the change here
            End If
        Next

        accid = accid.Remove(accid.Length - 1, 1)
        MsgBox(accid)
 End Sub

推荐阅读