|
-
Jan 23rd, 2011, 05:20 PM
#1
Thread Starter
Lively Member
Randomize Text from textbox into another textbox
I am creating a Map Rotation Randomizer for my Call of Duty clan, and I am having an issue with understanding how to randomize the text from the list and placing it into another textbox.
Here's what it looks like right now:

All textboxes are multi-line.
When I click a button for a game (CoD4, MW2, WaW), it places the map list into the left textbox like:
mp_bloc
mp_broadcast
mp_crossfire
etc.
When I click the button with ">>>", I want it to randomize that list and place it into the right textbox like:
map mp_bloc map mp_broadcast map mp_crossfire
or if I click it again:
map mp_crossfire map mp_bloc map mp_broadcast
and so on. I think I need to create an array, but I've never worked with those before as I'm still new to learning programming. I'm not even sure what controls I need to put on the form for that, if it even requires any.
If you need the code list or something let me know. Thank you in advance.
-
Jan 23rd, 2011, 05:38 PM
#2
Re: Randomize Text from textbox into another textbox
you wouldn't really use textboxes for this.. (at least the left side). how are you reading in the maps for each game? hard-coded? from a file?
EDIT: Try this out. Make the left side a ListBox and the right side a multi-line Textbox:
vb.net Code:
Public Class Form1 Dim MapListCod As New List(Of String) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Add COD Maps to List MapListCod.Add("mp_backlot") MapListCod.Add("mp_bloc") MapListCod.Add("mp_bog") MapListCod.Add("mp_cargoship") MapListCod.Add("mp_citystreets") MapListCod.Add("mp_convoy") MapListCod.Add("mp_countdown") MapListCod.Add("mp_crash") MapListCod.Add("mp_crash_snow") MapListCod.Add("mp_crossfire") MapListCod.Add("mp_farm") MapListCod.Add("mp_overgrown") MapListCod.Add("mp_pipeline") MapListCod.Add("mp_shipment") MapListCod.Add("mp_showdown") MapListCod.Add("mp_strike") MapListCod.Add("mp_vacant") MapListCod.Add("mp_broadcast") MapListCod.Add("mp_carentan") MapListCod.Add("mp_creek") MapListCod.Add("mp_killhouse") 'Add To ListBox ListBox1.Items.AddRange(MapListCod.ToArray) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Clear() 'Clear it in case they randomize more than once Dim rint As New Random For i As Integer = 0 To MapListCod.Count - 1 TextBox1.Text &= ListBox1.Items(rint.Next(0, ListBox1.Items.Count - 1)).ToString & " " Next End Sub End Class
Last edited by stateofidleness; Jan 23rd, 2011 at 09:17 PM.
-
Jan 23rd, 2011, 09:06 PM
#3
Thread Starter
Lively Member
Re: Randomize Text from textbox into another textbox
I rated your post. This is the most help I've ever gotten on this forum. I'm gonna go try this here in a bit, and I'll report back how it goes. Thanks a ton man!
EDIT > Yes, the map lists are hard-coded for each button (CoD4, MW2, WaW).
Last edited by SLeePYG72786; Jan 23rd, 2011 at 09:14 PM.
-
Jan 23rd, 2011, 09:18 PM
#4
Re: Randomize Text from textbox into another textbox
That code, if I'm not mistaken, allows for duplicates. Is that what you want?
That is, if your maps were "a", "b" and "c", you might end up with an ordering of "a", "b", "b" in your textbox.
If not, there are plenty of threads in this forums on random ordering of a set, so I won't rehash the same ground again, but suggest you use the search function.
-
Jan 23rd, 2011, 09:18 PM
#5
Re: Randomize Text from textbox into another textbox
ah good deal. you should be able to build off that to include the other two games.
-
Jan 24th, 2011, 08:27 AM
#6
Re: Randomize Text from textbox into another textbox
This
Dim rint As New Random
should be moved outside of the Sub.
-
Jan 24th, 2011, 08:42 AM
#7
Re: Randomize Text from textbox into another textbox
Code:
Dim prng As New Random
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim foo As New List(Of String)(TextBox1.Lines)
Dim bar As New List(Of String)
Do While foo.Count > 0
Dim idx As Integer = prng.Next(foo.Count)
bar.Add(foo(idx))
foo.RemoveAt(idx)
Loop
TextBox2.Lines = bar.ToArray
End Sub
Private Sub Form1_Shown(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Shown
'generate test map list
Dim MapListCod As New List(Of String)
For x As Integer = 1 To 10
MapListCod.Add("mp_" & x.ToString)
Next
TextBox1.Lines = MapListCod.ToArray
End Sub
-
Jan 24th, 2011, 09:34 AM
#8
Re: Randomize Text from textbox into another textbox
 Originally Posted by dbasnett
This
Dim rint As New Random
should be moved outside of the Sub.
YOU'RE outside the sub! lol
doesn't it limit its scope though?
-
Jan 24th, 2011, 09:41 AM
#9
Re: Randomize Text from textbox into another textbox
I would suggest that only one random object is needed per application. See the remarks section here
http://msdn.microsoft.com/en-us/libr...em.random.aspx
-
Jan 24th, 2011, 11:22 AM
#10
Re: Randomize Text from textbox into another textbox
 Originally Posted by dbasnett
I agree with dbasnett, but just pointing out that he is quite safe since no one can click a button so fast that it generates two random objects with the same date seed (we're talking ticks here).
-
Jan 24th, 2011, 07:25 PM
#11
Re: Randomize Text from textbox into another textbox
 Originally Posted by ForumAccount
I agree with dbasnett, but just pointing out that he is quite safe since no one can click a button so fast that it generates two random objects with the same date seed (we're talking ticks here).
I agree, that in this case, the code is protected by clicking speed. But, it is a bad habit. One random per app.
-
Jan 24th, 2011, 07:38 PM
#12
Re: Randomize Text from textbox into another textbox
Hmm no update yet from the OP. I know I overlooked the "repeat" items aspect, but the OP hasn't said if that's a deal breaker or not. I've used other COD4 map list generators and some actually DO allow repeating a map, but changing the gametype: so mp_bog mp_bog (could be Bog: Deathmatch and then Bog: Infiltration)
And to be honest, I've never actually used the Random class prior to this thread! So it's good practice to use it once in a single application?
-
Jan 24th, 2011, 07:40 PM
#13
Thread Starter
Lively Member
Re: Randomize Text from textbox into another textbox
Sorry everyone. I haven't gotten around to being able to get back into this project yet. Had so many other things going on. I'll try to get into it again soon. Real-life things at the moment.
But I'm glad that there are others that are in the CoD programming community. There aren't many out there.
-
Jan 25th, 2011, 07:31 AM
#14
Re: Randomize Text from textbox into another textbox
 Originally Posted by stateofidleness
Hmm no update yet from the OP. I know I overlooked the "repeat" items aspect, but the OP hasn't said if that's a deal breaker or not. I've used other COD4 map list generators and some actually DO allow repeating a map, but changing the gametype: so mp_bog mp_bog (could be Bog: Deathmatch and then Bog: Infiltration)
And to be honest, I've never actually used the Random class prior to this thread! So it's good practice to use it once in a single application?
What benefit would there be in having more than one? Try this:
Code:
Dim prng1 As New Random
Dim prng2 As New Random
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
For x As Integer = 1 To 10
Debug.WriteLine(prng1.Next(1000).ToString)
Debug.WriteLine(prng2.Next(1000).ToString)
Next
End Sub
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
|