|
-
Dec 9th, 2009, 04:27 PM
#1
Thread Starter
New Member
Adding Items From a List, Removing Items and randomly pairing two items from the list
Hi, I'm just getting into learning VB6. I have a slew of books and search the web, but still find it hard to find exactly what I'm looking for. I don't know if I just don't know exactly what I'm looking for is called or what, but I have finally decided to try and just post a specific question and see what I get.
This is just a project I'm working on to try and learn how to do something in VB. I find that I learn best if I have an objective and try to complete it and see how it all functions together to give me the results I wanted. What I'm trying to make is a program that will pair up names, but will not allow certain names to be paired together.
I'll first describe in general what I'm trying to do and then I'll go into more specific details.
So for the general description:
I want to create a list of names that can vary up to around 10 names or so.
I then want to prompt the user for each name which other name it should not be paired up with (if any). Of course the original name would not be paired up with itself, but also if there is any other name that it should not be paired up with. It will do that for each name and then will randomly pair up each name with another.
For example:
If the list of names is:
John
Max
Sam
Peter
Sara
Jane
Julie
Maria
And I don't want:
John to be paired with Sara
Max to be paired with Jane
Sam to be paired with Julie
Peter to be paired with Maria
After randomly selecting it could come up with something like:
John - Max
Max - Maria
Sam - Peter
Peter- Jane
Sara - Julie
Jane - John
Julie - Sara
Maria - Sam
So as you can see, I want to pair each name with another name, but the two names which should not be paired will never be paired together.
John can be paired with Max and then Max should be taken out of the list so that no other name can pair with him. But Max will still need to be paired with a name as well. It basically takes two lists of the same names and matches them up so that they're not paired with their own name and not paired with a name that was chosen for them not to pair with.
What I have so far is a Main Form.
At that form it has a command button to add names. When the command button is selected a new form appears and has a Text Box to enter a name and a command button to submit the name. It also has a nother command button to stop entering names.
If the submit button is clicked it will add the information from the Text field into a List on the first form and then another form just like it will appear requesting the second name. And so on and so on. Once 10 names have been reached or the user selects the "Stop entering names" button, it will go back to the main form.
Then on the main form there is a Command Button labeled Rules. If they click that button, it will bring up a form that says "Who should Sara (or whoever was the first name entered) not be paired with? Then there are radio buttons with all of the other names from the list except Sara.
This is pretty much where I'm at right now.
I'm stuck with what to do with the radio button. When a certain button is clicked I don't know how to remove that name from the list and then generate a new list for the next name that will remove the name that was just selected before as well as any other names.
Currently the Rules forms (to omit certain names from being paired) is pulling the name from the forms where the names were originally listed (not from the list). I didn't know how to select a name from the list once it's in there.
Any help would be greatly appreciated. Sample files would be great.
Last edited by Blain1976; Dec 9th, 2009 at 04:33 PM.
-
Dec 10th, 2009, 11:13 AM
#2
Re: Adding Items From a List, Removing Items and randomly pairing two items from the
I'm a little confused. In your example, you show 8 individuals paired into 8 pairings. Shouldn't 8 people only form 4 pairings? Or is this some sort of Secret Santa type thing where a 'pairing' is only one-way?
If it's one way, it might be better to use "->" instead of "-":
John -> Max
Max -> Maria
Also, when you say that one person cannot be matched with another, is that a one way roadblock, or does it work both ways?
And I don't want:
John to be paired with Sara
This prevents John -> Sara, but does it also prevent Sara -> John?
-
Dec 10th, 2009, 04:02 PM
#3
Thread Starter
New Member
Re: Adding Items From a List, Removing Items and randomly pairing two items from the
Hi, Thanks for your reply.
You are exactly right. It's to draw names for Secret Santa which is why there are 8 pairs (one way).
The not allowing certain people to be paired together would go both ways. It's to prevent spouses to draw each others names.
-
Dec 10th, 2009, 07:40 PM
#4
Re: Adding Items From a List, Removing Items and randomly pairing two items from the
I'd go with two listboxes. On the left the main listbox would hold all your names. On the right, a second listbox with checkbox style (allowing multi-select) would show all the names except the currently selected name from the main list. The user can then simply check whichever names to exclude. This second listbox would get completely repopulated (invoke the Clear method first!) in the first listbox's Click() event.
Your best bet for storing the data is probably a nested array, which means a user-defined type:
Code:
Public Type SantaType
Santa As String
Exclude() As String
End Type
Public data() As SantaType
Update the Exclude() arrays as the checkboxes get clicked in the right listbox. I think it's the Check() event. (Each change updates two arrays at a time, since both get added to each others' exclude list.)
For creating the actual pairs, first create a second boolean array to keep track of who has been paired up already (and therefore on everybody else's exclude list.) Iterate through the data() array one at a time. Pick a random match for this santa until you get one that is false in the boolean array AND is not in the exclude list AND is not the same index you're actually on. (Can't be your own secret santa.) Once identified, set the boolean array to true for randomly determined recipient and loop back to the next giver.
Normally I would just set this all up and post it since it'll end up being pretty small, but it sounds like you want to take a crack at it yourself as a teaching tool. As such, don't be shy with any questions. Note that I'm not saying my ideas for the user interface and data are the greatest ideas ever; it's simply my first instinct. (Which I tend to go with.)
Last edited by Ellis Dee; Dec 10th, 2009 at 07:45 PM.
-
Dec 14th, 2009, 01:49 PM
#5
Thread Starter
New Member
Re: Adding Items From a List, Removing Items and randomly pairing two items from the
I appreciate the replies.
I'm very, very close.
I have the form set up the way I want. I actually decided to make things easier (I think) and have one list of the names in the drawing and have another list that would hold names that should not be included at that time.
So for instance, If I were drawing for myself, I would move my name over to the second list and any other names that I didn't want me to draw and then I would draw from the first list randomly. Whichever name it selects, I would then remove. Then I would move my name and any other names in the second list back into the first list so that they could be picked by someone else.
I am having a problem though with moving the item in the first list over to the second list. Currently I'm selecting the item from the list and then clicking a command button to move it over. Except instead of moving the string, it's moving the List Index? I think?
So for instance if I click the third item in the list it enters "4" in the list at the right.
So any help with populating the second list with the actual string from the first list would be appreciated.
I think once I get that figured out, I'll be all set.
Thank you again for your replies.
-
Dec 14th, 2009, 01:57 PM
#6
Thread Starter
New Member
Re: Adding Items From a List, Removing Items and randomly pairing two items from the
I think I just figured it out.
lstTemp.AddItem List1.Text
Before I had:
lstTemp.AddItem List1.ListIndex
Looks like I'm all set now.
Thanks again for the help.
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
|