【问题标题】:Selected Listbox Items - to add data to array VB.net选定的列表框项目 - 将数据添加到数组 VB.net
【发布时间】:2020-11-08 10:14:44
【问题描述】:

努力测试选择了哪些列表框项。我想允许用户在列表框中选择多个项目,以便可以将字符串存储在还存储列表的数组中。这是一个点名系统(我 10 年级学生的任务)。我只是无法获得适合列表框的语法。列表框设置为 MultiSelection。

顺便说一句

If Listbox.SelectedItem = true Then

不起作用。它返回一个错误。

我下面的代码返回第一个选定的项目(在消息框中) - 但不是其他项目。我现在只是兜兜转转。必须有更简单的方法。想法?

Private Sub BtnRollCall1_Click(sender As Object, e As EventArgs) Handles btnRollCall1.Click
    Dim ExcursionArray(29, 4) As String
    Dim selected As Integer
    Dim LoadNames As StreamReader = File.OpenText("ClassList.txt")
    For i = 0 To 29
        ExcursionArray(i, 0) = (LoadNames.ReadLine())
        lbxRollCall.Items.Add(ExcursionArray(i, 0))
    Next

    For Each SelectedItem As string In lbxRollCall.SelectedItems
        selected = lbxRollCall.SelectedIndex
        ExcursionArray(selected, 1) = "a"
    Next

    For x = 0 To 29
        If (ExcursionArray(x, 1) = "a") Then
            MsgBox(ExcursionArray(x, 0))
        End If

    Next
End Sub

【问题讨论】:

    标签: vb.net selecteditem listboxitem


    【解决方案1】:

    如果在ListBox 中选择了相应的“行”,您实际上想要做的是更新一个二维数组并将第二个“列”设置为“a”。一种方法是这样的:

    For Each selectedIndex In lbxRollCall.SelectedIndices
        ExcursionArray(selectedIndex, 1) = "a"
    Next
    

    另一种选择是这样的:

    For i = 0 To ExcursionArray.GetUpperBound(0)
        If lbxRollCall.GetSelected(i) Then
            ExcursionArray(i, 1) = "a"
        End If
    Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多