Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Different lines from a ListBox to different variables

Hello!
I'm wondering how I can get the contents of a ListBox to a variable? There can be several lines in the listbox. It would also be ok to have every line stored in different variables. The lines are put into the ListBox with a TextBox and a button. When the button is pushed the contents of the TextBox is moved to the Listbox. Every time the button is pushed a new line is added.
0
nvenho
Asked:
nvenho
1 Solution
 
nvenhoAuthor Commented:
I have to increase the points with my last five points...
0
 
AlexFMCommented:
Private Sub Form_Load()
    List1.AddItem "Item 1"
    List1.AddItem "Item 2"
    List1.AddItem "Item 3"
End Sub

Private Sub Command1_Click()
    Dim s As String
    Dim i As Integer
   
    For i = 0 To List1.ListCount
        s = s + List1.List(i) + vbCrLf
    Next
   
    MsgBox s
End Sub

Form has List1 ListBox abd Command1 button.
0
 
vinnyd79Commented:
'add listbox contents to an array
Private Sub Command1_Click()
Dim i As Integer
Dim arrList() As String ' array to hold listitems

For i = 0 To List1.ListCount - 1
    ReDim Preserve arrList(i)
    arrList(i) = List1.List(i)
Next i

' array will contain
' arrList(0) = 1st item in listbox
' arrList(1) = 2nd item in listbox
' arrList(2) = 3rd item in listbox

' loop through array
For i = 0 To UBound(arrList)
    MsgBox arrList(i)
Next i


End Sub

' add listbox contents to a variable
Private Sub Command2_Click()
Dim i As Integer
Dim ItmList As String
For i = 0 To List1.ListCount - 1
If ItmList <> "" Then
    ItmList = ItmList & vbCrLf & List1.List(i)
Else
    ItmList = List1.List(i)
End If
Next i
MsgBox ItmList

End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is an example of how to store the items in a collection.  Create a project and add one textbox, one, commandbutton, and two listboxes.  Each an entry is in the textbox and enter is pressed it will be added to the first listbox and the collection.  When the commandbutton is pressed, the second listbox will be cleared and then it will be populated with the contents of the collection.

Dim listItems As Collection

Private Sub Command1_Click()
    Dim item As Variant
   
    List2.Clear
    For Each item In listItems
        List2.AddItem item
    Next item
End Sub

Private Sub Form_Load()
    Text1.Text = ""
    Set listItems = New Collection
    Command1.Caption = "Enumerate listItems"
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        If Len(Trim(Text1.Text)) > 0 Then
            List1.AddItem Text1.Text
            listItems.Add Text1.Text
            Text1.Text = ""
            KeyAscii = 0
        End If
    End If
End Sub
0
 
nvenhoAuthor Commented:
I have to give the points to vinnyd79 because the example he gave is just what I need. I would split them but I think there is no idea to split 45 points and don't have more to give.

Thanks to you all!
0
 
nvenhoAuthor Commented:
Hello Again!
I forgot to ask about this in my original question. I'm using this code to remove lines from the ListBox. Is it possible to remove the same line from the variable I'm using in the example vinnyd79 wrote. I would like to have the variable "updated" if a line is removed from ListBox...

Private Sub displacement_remove_Click()
    Dim ind As Integer
   
    ind = forced_displacement.ListIndex      'take listindex
    If ind >= 0 Then                         'check that line is selected
        forced_displacement.RemoveItem ind   'remove line
    Else
        Beep
    End If

End Sub
0
 
nvenhoAuthor Commented:
I already found a solution

dim storevar as string
sub getvar()
Dim i As Integer
Dim ItmList As String
For i = 0 To List1.ListCount - 1
If ItmList <> "" Then
ItmList = ItmList & vbCrLf & List1.List(i)
Else
ItmList = List1.List(i)
End If
Next i
Storevar=ItmList
end sub
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now