Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Different lines from a ListBox to different variables

Posted on 2003-11-01
7
Medium Priority
?
249 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 180 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
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.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

715 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