【问题标题】:VB.NET Get the values of a multi-select list boxVB.NET 获取多选列表框的值
【发布时间】:2018-01-21 09:52:59
【问题描述】:

我有一个启用了 milti 选择的列表框,我试图根据选定的值创建一个 TSQL 命令,我从列表框中获取第一个选定的值,我如何获取其余选定的值。

Private Sub createSQLCMD(ByVal strServerName As String, ByVal 
strDatabaseName As String, ByVal strTableName As String)

    Dim strSQLCMD As String = "SELECT "

    For i As Integer = 0 To lstFieldList.Items.Count - 1
        If (lstFieldList.GetSelected(i)) And i < lstFieldList.Items.Count - 1 Then
            strSQLCMD &= lstFieldList.SelectedValue.ToString & ","

        End If
    Next
        strSQLCMD &= lstFieldList.SelectedValue.ToString
        txtSQLCMD.Text = strSQLCMD & " FROM " & strTableName
End Sub

【问题讨论】:

  • ListBox 类中实际上没有内置任何东西来做到这一点。有一个 SelectedItems 属性是单数 SelectedItem 的倍数,但没有 SelectedValues 等价于 SelectedValueCheck this out 寻求解决方案。

标签: windows vb.net winforms


【解决方案1】:
Private Sub createSQLCMD(ByVal strServerName As String, ByVal strDatabaseName As String, ByVal strTableName As String)
    Dim strSQLCMD As String = "SELECT "

    Dim selectedItems = lstFieldList.SelectedItems.Cast(Of String)

    'Dont forget to check if no item selected else you'll get an error.
    txtSQLCMD.Text = strSQLCMD & String.Join(",", selectedItems) & " FROM " & strTableName
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 2013-07-15
    • 2017-01-07
    • 2017-03-09
    • 1970-01-01
    • 2013-10-21
    相关资源
    最近更新 更多