Results 1 to 14 of 14

Thread: Randomize Text from textbox into another textbox

  1. #1

    Thread Starter
    Lively Member SLeePYG72786's Avatar
    Join Date
    Sep 2010
    Posts
    66

    Question 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.

  2. #2
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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:
    1. Public Class Form1
    2.  
    3.     Dim MapListCod As New List(Of String)
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         'Add COD Maps to List
    7.         MapListCod.Add("mp_backlot")
    8.         MapListCod.Add("mp_bloc")
    9.         MapListCod.Add("mp_bog")
    10.         MapListCod.Add("mp_cargoship")
    11.         MapListCod.Add("mp_citystreets")
    12.         MapListCod.Add("mp_convoy")
    13.         MapListCod.Add("mp_countdown")
    14.         MapListCod.Add("mp_crash")
    15.         MapListCod.Add("mp_crash_snow")
    16.         MapListCod.Add("mp_crossfire")
    17.         MapListCod.Add("mp_farm")
    18.         MapListCod.Add("mp_overgrown")
    19.         MapListCod.Add("mp_pipeline")
    20.         MapListCod.Add("mp_shipment")
    21.         MapListCod.Add("mp_showdown")
    22.         MapListCod.Add("mp_strike")
    23.         MapListCod.Add("mp_vacant")
    24.         MapListCod.Add("mp_broadcast")
    25.         MapListCod.Add("mp_carentan")
    26.         MapListCod.Add("mp_creek")
    27.         MapListCod.Add("mp_killhouse")
    28.  
    29.         'Add To ListBox
    30.         ListBox1.Items.AddRange(MapListCod.ToArray)
    31.     End Sub
    32.  
    33.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    34.         TextBox1.Clear() 'Clear it in case they randomize more than once
    35.         Dim rint As New Random
    36.         For i As Integer = 0 To MapListCod.Count - 1
    37.             TextBox1.Text &= ListBox1.Items(rint.Next(0, ListBox1.Items.Count - 1)).ToString & " "
    38.         Next
    39.     End Sub
    40. End Class

  3. #3

    Thread Starter
    Lively Member SLeePYG72786's Avatar
    Join Date
    Sep 2010
    Posts
    66

    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.

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  5. #5

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Randomize Text from textbox into another textbox

    This

    Dim rint As New Random

    should be moved outside of the Sub.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Randomize Text from textbox into another textbox

    Quote Originally Posted by dbasnett View Post
    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?

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Randomize Text from textbox into another textbox

    Quote Originally Posted by dbasnett View Post
    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
    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).

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Randomize Text from textbox into another textbox

    Quote Originally Posted by ForumAccount View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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?

  13. #13

    Thread Starter
    Lively Member SLeePYG72786's Avatar
    Join Date
    Sep 2010
    Posts
    66

    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.

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Randomize Text from textbox into another textbox

    Quote Originally Posted by stateofidleness View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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