Results 1 to 12 of 12

Thread: [RESOLVED] randomizing words in list1

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Resolved [RESOLVED] randomizing words in list1

    list1 items

    tool box
    machine gun
    full magazines
    dvd movies
    pizza box
    air planes

    this list can be big for now this is simple list , i need a code to switch the string to this order

    box tool
    gun machine
    magazines full
    movies full
    movies dvd
    box pizza
    planes air

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: randomizing words in list1

    That's not randomizing.

    Just break each word-phrase into an array using Split(word_phrase, " ") then start at the highest index to 0 and put the pieces back together and add a space at each iteration thru the loop


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: randomizing words in list1

    Use this function

    Code:
    Private Function ReverseSentence(ByVal s As String) As String
    
        Dim a() As String
        Dim j As Long
        Dim strRet As String
        
        a = Split(s, " ")
        
        For j = UBound(a) To 0 Step -1
            strRet = strRet & a(j) & " "
        Next
        
        ReverseSentence = Trim$(strRet)
    
    End Function



  4. #4

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: randomizing words in list1

    how do i call this from button click event

    Private Function ReverseSentence(ByVal s As String) As String

    Dim a() As String
    Dim j As Long
    Dim strRet As String

    a = Split(s, " ")

    For j = UBound(a) To 0 Step -1
    strRet = strRet & a(j) & " "
    Next

    ReverseSentence = Trim$(strRet)

    End Function

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: randomizing words in list1

    You would call it by passing the string you want to modify and get the result which you would then place into your list.

    Most times when you see this done in a list there will also be a comma after the first word to show that the order has been changed with the last word first for sorting purposes.

    i.e. LastName, FirstName

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: randomizing words in list1

    how do i call this from button click event
    Code:
    Private Sub Command1_Click()
        
        Dim j As Long
        
        List2.Clear
        
        For j = 0 To List1.ListCount - 1
            List2.AddItem (ReverseSentence(List1.List(j)))
        Next
        
    End Sub
    Change the output to any desired control instead of List2



  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: randomizing words in list1

    vb Code:
    1. 'Add two Listbox and a commandbutton
    2. Option Explicit
    3. Dim s As String
    4. Private Function ReverseSentence(ByVal s As String) As String
    5.  
    6.     Dim a() As String
    7.     Dim j As Long
    8.     Dim strRet As String
    9.    
    10.     a = Split(s, " ")
    11.    
    12.     For j = UBound(a) To 0 Step -1
    13.         strRet = strRet & a(j) & " "
    14.     Next
    15.     ReverseSentence = Trim$(strRet)
    16. End Function
    17.  
    18. Private Sub Command1_Click()
    19. Dim a As Integer
    20. For a = 0 To List1.ListCount - 1
    21.  List2.AddItem ReverseSentence(List1.List(a))
    22. Next a
    23.  
    24. End Sub
    25.  
    26. Private Sub Form_Load()
    27.  Command1.Caption = "Switch words"
    28. List1.AddItem "tool box"
    29. List1.AddItem "machine gun"
    30. List1.AddItem "full magazines"
    31. List1.AddItem "dvd movies"
    32. List1.AddItem "pizza box"
    33. List1.AddItem "air planes"
    34. End Sub
    Last edited by Nightwalker83; Jul 13th, 2013 at 09:08 PM. Reason: Fixed error in code!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: randomizing words in list1

    @Nightwalker83

    Why you put List2.AddItem ReverseSentence inside the function?

    Such function must be generic and don't refer to any control or using the returned value inside it!



  9. #9
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: randomizing words in list1

    Quote Originally Posted by 4x2y View Post
    @Nightwalker83

    Why you put List2.AddItem ReverseSentence inside the function?

    Such function must be generic and don't refer to any control or using the returned value inside it!
    I have forgotten how to return it properly.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: randomizing words in list1

    This line returns the result
    Code:
    ReverseSentence = Trim$(strRet)



  11. #11
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: randomizing words in list1

    Quote Originally Posted by 4x2y View Post
    This line returns the result
    Code:
    ReverseSentence = Trim$(strRet)
    I have fixed the code in post #7 the line I was looking for was

    vb Code:
    1. List2.AddItem ReverseSentence(List1.List(a))
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  12. #12

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: randomizing words in list1

    thanks guyz appreciate it

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