Solved

Different lines from a ListBox to different variables

Posted on 2003-11-01
7
245 Views
Last Modified: 2012-05-04
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
Comment
Question by:nvenho
7 Comments
 

Author Comment

by:nvenho
ID: 9663022
I have to increase the points with my last five points...
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9663139
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
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 45 total points
ID: 9663141
'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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9663411
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
 

Author Comment

by:nvenho
ID: 9664232
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
 

Author Comment

by:nvenho
ID: 9664647
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
 

Author Comment

by:nvenho
ID: 9665752
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question