Results 1 to 7 of 7

Thread: Arrays

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    154

    Arrays

    I would like to load a lsit and have it in an array but im new and havent done alot with an array so for practice I want to laod a lsit and then when I press I command button it laods everything in the array and puts it in List1...Thx very much guys I aprepciate it

  2. #2
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    154
    Hmm thx man I appreciate it but im very new and stupid with vb so could u show a clear code of laoding an array with a command button and then dispalying whats in it in list1?Thx bro

  4. #4
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    VB Code:
    1. Dim ListArray() As String 'in the genereal declerations section of the form...this is above all other code
    2.  
    3. 'Then in the click event add the items
    4. Private Sub Command1_Click()
    5. ReDim ListArray(0) As String 'Create the array
    6.  
    7.  
    8. ReDim Preserve ListArray(UBound(ListArray) + 1)'Add a new element to the array
    9. ListArray(UBound(ListArray)) = "First Item" 'fill that new element with a string
    10. ReDim Preserve ListArray(UBound(ListArray) + 1)
    11. ListArray(UBound(ListArray)) = "Second Item"
    12. ReDim Preserve ListArray(UBound(ListArray) + 1)
    13. ListArray(UBound(ListArray)) = "Third Item"
    14. ReDim Preserve ListArray(UBound(ListArray) + 1)
    15. ListArray(UBound(ListArray)) = "Fourth Item"
    16. ReDim Preserve ListArray(UBound(ListArray) + 1)
    17. ListArray(UBound(ListArray)) = "Fith Item"
    18. ReDim Preserve ListArray(UBound(ListArray) + 1)
    19. ListArray(UBound(ListArray)) = "Sixth Item"
    20.  
    21.  
    22. For i = 1 To UBound(ListArray) 'loop thru the arrray and add the items to the listbox.
    23. List1.AddItem (ListArray(i))
    24. Next
    25. End Sub
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    154
    Originally posted by Arc
    VB Code:
    1. Dim ListArray() As String 'in the genereal declerations section of the form...this is above all other code
    2.  
    3. 'Then in the click event add the items
    4. Private Sub Command1_Click()
    5. ReDim ListArray(0) As String 'Create the array
    6.  
    7.  
    8. ReDim Preserve ListArray(UBound(ListArray) + 1)'Add a new element to the array
    9. ListArray(UBound(ListArray)) = "First Item" 'fill that new element with a string
    10. ReDim Preserve ListArray(UBound(ListArray) + 1)
    11. ListArray(UBound(ListArray)) = "Second Item"
    12. ReDim Preserve ListArray(UBound(ListArray) + 1)
    13. ListArray(UBound(ListArray)) = "Third Item"
    14. ReDim Preserve ListArray(UBound(ListArray) + 1)
    15. ListArray(UBound(ListArray)) = "Fourth Item"
    16. ReDim Preserve ListArray(UBound(ListArray) + 1)
    17. ListArray(UBound(ListArray)) = "Fith Item"
    18. ReDim Preserve ListArray(UBound(ListArray) + 1)
    19. ListArray(UBound(ListArray)) = "Sixth Item"
    20.  
    21.  
    22. For i = 1 To UBound(ListArray) 'loop thru the arrray and add the items to the listbox.
    23. List1.AddItem (ListArray(i))
    24. Next
    25. End Sub
    Oh wow ok thx very much bro so this:For i = 1 To UBound(ListArray) 'loop thru the arrray and add the items to the listbox.
    List1.AddItem (ListArray(i))
    Nextthat goes into a command button of where I want to add the items to list1 and then:Private Sub Command1_Click()
    ReDim ListArray(0) As String 'Create the array


    ReDim Preserve ListArray(UBound(ListArray) + 1)'Add a new element to the array
    ListArray(UBound(ListArray)) = "First Item" 'fill that new element with a string
    ReDim Preserve ListArray(UBound(ListArray) + 1)
    ListArray(UBound(ListArray)) = "Second Item"
    ReDim Preserve ListArray(UBound(ListArray) + 1)
    ListArray(UBound(ListArray)) = "Third Item"
    ReDim Preserve ListArray(UBound(ListArray) + 1)
    ListArray(UBound(ListArray)) = "Fourth Item"
    ReDim Preserve ListArray(UBound(ListArray) + 1)
    ListArray(UBound(ListArray)) = "Fith Item"
    ReDim Preserve ListArray(UBound(ListArray) + 1)
    ListArray(UBound(ListArray)) = "Sixth Item"

    then does that go in a comamnd button to laod an array?Thx for the help bro

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    154
    O damn sorry I got it im very stupid and that worked really good!!!So thx very much but in 1 command button I want to load a txt file and have it in an array...So just like what u did but be able to load a file from your computer and have it in there(array) then click the comamnd button and add them all to your lsit box...So could u help me out ehre plz?Thx man I aprpeciate it

  7. #7
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Well to put a txt file into an array i would use the Split function.
    Input the txt file and store its contents in a Variable, then use the Split function to break it into peices and put it in an array. You need to decide how you want to split it up... You want to split it everywhere there is a Space, or a period, or the letter i... it's up to you. THat is what is called Delimiter.

    VB Code:
    1. Dim MyArray() as string 'Create the array
    2. MyArray = Split(MyString,"The Delimiter") 'As i said before the Delimter could be anything from a space " ", to a whole sentence "A sentence".
    3.  
    4. 'Now you have an Array called MyArray
    5.  
    6. 'To loop thru this array and add it to a listbox you do the same i showed you before.
    7.  
    8. For i = 0 to Ubound(MyArray)
    9. List1.additem(MYarray(i))
    10. Next
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width