Solved

Different lines from a ListBox to different variables

Posted on 2003-11-01
7
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 86

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

Technology Partners: 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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 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