J Talati J Talati - 1 year ago 43
Vb.net Question

How to create an Array List with Listbox2 Texts items which are added by listbox1

My Global Variable Class:

Public Class GlobalVariable
Public Shared containsListBox2Item As ArrayList
End class

My Listbox2 Items are: What i am doing is adding listbox 2 items from listbox 1 using a > button which i need all items from listbox2 into an array list and then call it to do another thing. so i want to call each item. not sure how to do so.

1. X,Y Coordinate
2. Latitude, Longitude
3. Zip Code
4. State
5. City

Basically i want each listbox.text item to have its own value in the array list. so lets say like

If listbox1.text = "X,Y Coordinate" Then
GlobalVariable.containsListBox2Item.add("X,Y Coordinate").

Not sure if i coded that properly. Please help.

Answer Source

I would store the items not in the ListBox controls, but in Lists. In general, you want to keep your data somewhere not on the UI thread but just use the UI as a display. Ultimately, you will put the items from a List into the ArrayList

Make a winforms project, add a two ListBox and two Button controls.

Private list1 As New List(Of String)
Private list2 As New List(Of String)

Private arrayList As New ArrayList()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Button1.Text = ">"
    Button2.Text = "Finish"
    list1.AddRange({"X, Y", "Lat", "Zip", "State", "City"})
End Sub

Private Sub updateListBoxes()
End Sub

' > button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim item = ListBox1.Text
    If item = "" Then Exit Sub
End Sub

' finish button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub