-
Jul 11th, 2013, 02:39 PM
#1
Thread Starter
Banned
[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
-
Jul 11th, 2013, 04:26 PM
#2
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.
-
Jul 11th, 2013, 04:28 PM
#3
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
-
Jul 12th, 2013, 12:49 AM
#4
Thread Starter
Banned
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
-
Jul 12th, 2013, 02:23 AM
#5
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
-
Jul 12th, 2013, 04:54 PM
#6
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
-
Jul 13th, 2013, 01:09 AM
#7
Re: randomizing words in list1
vb Code:
'Add two Listbox and a commandbutton Option Explicit Dim s As String 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 Private Sub Command1_Click() Dim a As Integer For a = 0 To List1.ListCount - 1 List2.AddItem ReverseSentence(List1.List(a)) Next a End Sub Private Sub Form_Load() Command1.Caption = "Switch words" List1.AddItem "tool box" List1.AddItem "machine gun" List1.AddItem "full magazines" List1.AddItem "dvd movies" List1.AddItem "pizza box" List1.AddItem "air planes" 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
-
Jul 13th, 2013, 05:45 PM
#8
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!
-
Jul 13th, 2013, 06:08 PM
#9
Re: randomizing words in list1
Originally Posted by 4x2y
@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
-
Jul 13th, 2013, 06:50 PM
#10
Re: randomizing words in list1
This line returns the result
Code:
ReverseSentence = Trim$(strRet)
-
Jul 13th, 2013, 06:57 PM
#11
Re: randomizing words in list1
Originally Posted by 4x2y
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:
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
-
Jul 14th, 2013, 03:39 PM
#12
Thread Starter
Banned
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|