Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello. I am creating a program where you choose from the following radio boxes, '1v1,2v2,3v3,and 4v4', they are all in a group box. Now here comes the tricky part. Whenever I click, for example, 2v2 (radiobox2) i want there to be two Listboxes, and each Listbox displays two random names from a certain list, but each two cannot have the same name. :confused: For Example:

 

I click on radiobox2.

Each Listbox randomly displays two names from a given source. (names: jim, bob, alex, mark, dan, rob, pat)

So, Listbox1 displays MARK and PAT (randomly) and Listbox2 displays JIM and DAN(randomly).

As you see, both listbox's cannot have the same name. For instance, when they draw randomly Listbox1 and Listbox2 cannot both of mark, pat, dan, etc.

 

If you guys think there is a better way to do what i'm thinking (for instance, with something besides listbox's) please tell me. Any answer will be highly appreciated. :D

Posted
Simple - randomly pick two for the first list, and after that is done randomly pick names from the list, over and over till the drawen name dosn't exist in the first list and then add it.
Latly it would seem as though I'm don't abnegate from anything... except women. :( :)
Posted

it's not as easy as it sounds...

 

can you give me code for it please? is what you are talking about it randomly draws 2 names from a text file and displays it in the listbox?

 

i know how to randomly pick a number...

CStr(Int(Rnd() * 10)) 'Pick The Numbers Randomly 1-10

 

....but not randomly draw certain text (bill,joe,john,etc.)

Posted

This is off the top of my head (after a few beverages) ..so.... :)

If the source for the names is a text file for example, read each name into an array.

Once you have the array, Rnd within the bounds of the array to obtain the index value.

Add the name from the newly ranomized index value, you can then delete that from the array so it does not appear in the 2nd list, or if you need to keep that in the array, keep going through until the values dont mtach for the 2nd set as GMmorris had posted

Posted

Thanks Man. :D

 

But, I havn't worked with arrays (i'm pretty sure), nor text files (being the source) :o . If you can post code that would be of great help, or do something to make it easier for me to learn. Thanks.

 

BTW, i'm using Visual Basic .NET 2003 ;)

Posted

This is not the prettiest, but it should get you started:

 

Dim sr As StreamReader

sr = File.OpenText("The_File_With_the_Names.txt")

'read into an array

Dim mNameArray As New ArrayList()

Dim x As String

While sr.Peek <> -1

x = sr.ReadLine()

mNameArray.Add(x)

End While

sr.Close()

'now you have everything in the array - get the upper bounds of the array

Dim mCount as integer=mNameArray.Count

Dim Loc1 as integer

Dim Name1 as string

Dim Name2 as string

'now do your RND

Loc1=Int(Rnd() * mCount-1)

'get the name

Name1=mNameArray(Loc1)

'remove the name from the array

mNameArray.RemoveAt(Loc1)

'do it again for the 2nd name

'looping through all 4 would be nice - but for the sake of example we wont

Loc1=Int(Rnd() * mCount-1)

'get the name

Name2=mNameArray(Loc1)

'remove the name from the array

mNameArray.RemoveAt(Loc1)

'do the same with name3 and name4 if you want

' add them to a list box or whatever

Posted (edited)

wow! thanks man :D

 

There is some problem with it. 'StreamReader' is not declared (what is it for?). And when you say 'File.TextOpen' the 'file' is not defined. there is no option 'file', for when I uploaded a txt file inside my document with names. How would I go about adding to a listbox?

 

BTW, would I put the code for the radiobox?

Edited by whosyodaddy
Posted

Alright, thanks decrypt, that works for the file and streamreader. Although, how would I go about adding to a listbox? And would I put this code for the radiobox? Thanks. For the file here is my code:

 

System.IO.File.OpenText("SRC_Tournament_Randomizer.srcnames.txt")
Posted
I know the first answer: listbox1.Items.Add(item)

 

Ahh.. yes I forgot about that. Thanks. It works.

The only problem is the opening of the .txt file.

 

This isn't working:

sr = System.IO.File.OpenText("srcnames.txt")

 

Nor This:

sr = System.IO.File.OpenText("SRC_Tournament_Randomizer.rcnames.txt")
Posted (edited)
Ahh.. yes I forgot about that. Thanks. It works.

The only problem is the opening of the .txt file.

 

This isn't working:

 

 

Nor This:

 

I often forget that too and it takes me about 15minutes to remember. :D Edit: so your trying to open and read a file? Then do this:

 

             Dim oFile As System.IO.File
           Dim oRead As System.IO.StreamReader
           oRead = oFile.OpenText("C:\sample.txt")  ' or something like that

 

Edit: Also i don't see an srcnames anywhere in your script... edit: ah that's the text file nvm...

Edited by decrypt
Posted (edited)
I often forget that too and it takes me about 15minutes to remember. :D I too am trying to figure about how to open up a .txt file' date=' i'll tell you if i find out...[/quote']

 

thanks. remember: if the text file has to be content, embedded resource, etc. I try all of them and whenever I do this:

 

System.IO.File.OpenText(srcnames.txt)

 

it says 'srcnames' is not declared.

same with when I add the rootspace name :(

 

System.IO.File.OpenText("srcnames.txt")

 

I even add quotations and the System.IO.File.OpenText doesn't work. But, when I do add quotations it solves the 'srcnames' is not declared problem.

 

After trying many things, I think I know the problem. It is looking for the .txt file in where the project is saved (root folder) such as C:\My Projects\projectname\srcnames.txt. It isn't looking for it inside the project. I will try that, and can you give code to search for inside the project? Thanks.

 

I really appreciate ALL of you guys help so far.

Edited by whosyodaddy
Posted
Yeah that would probably happen, that's why whenever i'm making something for myself i always do the whole directory. Example: C:\Program files\Something\someting.txt
Posted (edited)
Yeah that would probably happen' date=' that's why whenever i'm making something for myself i always do the whole directory. Example: C:\Program files\Something\someting.txt[/quote']

 

 

Ok, that was the problem. Thanks guys :) I'm working on the script so far... for some reason it always starts with the same two people at first. :confused:

Edited by whosyodaddy
Posted (edited)
Heres something I slapped together to give you some ideas of things to play with.

It's not pretty (like me), but works (unlike me).

 

Thanks for that. I'm looking into it right now, but it always starts out with the same two people at first when you launch the application. Why is that? :confused: Your program is fantastic. But yours opens a text file into a listbox, then haves two listbox's gather text randomly from that. The way i'm trying to do it is different -- making it so when you click on radiobox1 (in a group box) it loads it into a listbox.

 

Here is my script so far:

 

Dim sr As System.IO.StreamReader

sr = System.IO.File.OpenText("srcnames.txt")

Dim mNameArray As New ArrayList 'read into an array

Dim x As String

While sr.Peek <> -1

x = sr.ReadLine()

mNameArray.Add(x)

End While

sr.Close()

'now you have everything in the array - get the upper bounds of the array

Dim mCount As Integer = mNameArray.Count

Dim Loc1 As Integer

Dim Name1 As String

Dim Name2 As String

Loc1 = Int(Rnd() * mCount - 1) 'now do your RND

Name1 = mNameArray(Loc1) 'get the 1rst name

mNameArray.RemoveAt(Loc1) 'remove the name from the array

Loc1 = Int(Rnd() * mCount - 1) 'looping through all 4 would be nice - but for the sake of example we wont

Name2 = mNameArray(Loc1) 'get the 2nd name

mNameArray.RemoveAt(Loc1) 'remove the name from the array

ListBox1.Items.Add(Name1) 'add them to listbox1

ListBox2.Items.Add(Name2) 'add them to listbox2

 

The things in bold is what i don't understand. What does 'loc1' stand for? when they say '-1' after mCount what does that mean. What about '-2' after it? Thanks.

Edited by whosyodaddy
Posted

While sr.Peek <> -1

Peek willl return a -1 if no more characters are avilable

 

Dim mCount As Integer = mNameArray.Count

Stores the size of the array

 

Dim Loc1 As Integer

is a variable that will hold our randon mumber. We will later use Loc1 to get the name stored in that location (or index) in the array.

 

Loc1 = Int(Rnd() * mCount - 1)

As arrays are zero based, so we need to use count-1 to ensure we dont go one over eg: an array of 10 items will be numbered 0-9

 

Name1 = mNameArray(Loc1)

We are getting the value(Name1) from the array index (loc1) that was generated by our RND

 

mNameArray.RemoveAt(Loc1)

Just so we dont repeat, lets delete the value we just used from the array

 

Does that make sense, or have I made it worse?

Posted
Does that make sense' date=' or have I made it worse?[/quote']

 

I don't know if you are being rhetorical or anything, but that actually made a little sense. Although, you mentioned a 'peek' what's that? Does anybody know why it always displays the same first two names all the time? Thanks.

 

PS. vcvc and Decrypt, I don't know where I would be without you! Where did you learn this stuff? From what books? The only book I have is 'Visual Studio .NET 2003 Step By Step'.

Posted
I learned vb by looking at examples. I took the examples and i edited them. After spending some time upgrading the examples i was pretty good in less than two years. I also learned some basics by reading the first four chapters of Visual basic 5 Step by Step lol.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...