Solved

Different lines from a ListBox to different variables

Posted on 2003-11-01
7
241 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now