Browse DevX
Sign up for e-mail newsletters from DevX


VB.NET Faces Off Against Classic VB : Page 10

VB.NET, the .NET Framework, and Visual Studio.NET together form a potent combination that can simplify your code and reduce errors; however, to truly supplant existing versions of VB as a RAD tool, Visual Studio needs Break-Edit-Continue.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Retrieve multiple selected items from a ListBox
In both classic VB and VB.NET, you can create a ListBox that lets users make multiple selections. There are two types of multiple selection—simple and extended. These examples use extended selection, which lets a user make multiple non-sequential selections by holding the control key down while clicking an item. In classic VB, set the MultiSelect property of the ListBox control to 2 (Extended). In VB.NET set the SelectionMode property to MultiExtended. A button on the form creates an array containing the user's selections and passes the array to a subroutine that displays the items. The following code shows the Click event for the buttons.

Classic VB

   Private Sub cmdGetListSelections_Click()
      Dim i As Integer
      Dim sArr() As String
      Dim selCount As Integer
      For i = 0 To List1.ListCount - 1
         If List1.Selected(i) Then
            ReDim Preserve sArr(selCount) As String
            sArr(selCount) = List1.List(i)
            selCount = selCount + 1
         End If
      Call showStringArray(sArr)
   End Sub
   Private Sub showStringArray(arr() As String)
      Dim s As String
      Dim i As Integer
      Dim arrCount As Integer
      On Error Resume Next
      arrCount = UBound(arr) + 1
      On Error GoTo 0
      If arrCount > 0 Then
         For i = 0 To arrCount - 1
            s = s & arr(i) & vbCrLf
         MsgBox s
      End If
   End Sub


   Private Sub btnGetListSelections_Click(ByVal sender _
      As System.Object, ByVal e As System.EventArgs) _
      Handles btnGetListSelections.Click
      Dim sArr(ListBox1.SelectedItems.Count - 1) _
         As String
      Dim anItem As String
      Dim i As Integer
      For Each anItem In ListBox1.SelectedItems
         sArr(i) = anItem
         i +=1
   End Sub
   Private Sub showStringArray(ByVal arr() As String)
      MessageBox.Show(Join(arr, _
   End Sub
   End Class

Thanks for your registration, follow us on our social networks to keep up-to-date