| Author |
Message |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2984 Location: Earth
|
Posted: Thu Nov 01, 2007 10:29 am Post subject: vb6 randomize files |
|
|
On a program i'm currently making i have it so that data is taken from a random files using this method
| Code: | Randomize
missionnum = (Int((3 - 1) * Rnd) + 1)
Select Case missionnum
Case 1
Open App.Path & "\easy\mission1.txt" For Input As #1
Case 2
Open App.Path & "\easy\mission2.txt" For Input As #1
End Select
Input #1, mission, level, description, goals
Close #1 |
Is it possible to make it so that rather than put each text file in manually, it automatically looks through the folder and picks a random file _________________ Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG
Caretaker - A Star Trek Voyager TCG |
|
| Back to top |
|
| |
Necromis Lifeless on my Boat

Joined: 11 Apr 2005 Posts: 767 Location: USA
|
Posted: Thu Nov 01, 2007 12:22 pm Post subject: |
|
|
well based on your data I would suggest a simple change, since the file name is a numerically different than the next. If you are truly using mission1.txt, mission2.txt and the so forth. I would do it with this change.
| Code: |
Randomize
missionnum = (Int((3 - 1) * Rnd) + 1)
Select Case missionnum
Case 1
Open App.Path & "\easy\mission" & missionnum & ".txt" For Input As #1
Case 2
Open App.Path & "\easy\mission" & missionnum & ".txt" For Input As #1
End Select
Input #1, mission, level, description, goals
Close #1
|
Use your numeric variable to select the file by its number. However, this only works if you are using that mission1.txt, mission2.txt file name format.
Edit: of course you don't actually have to do a select/case for this either. You could use a do/while or do/until, or for/next. Such as this.
| Code: |
For I = 1 to 100
missionnum = (Int((3 - 1) * Rnd) + 1)
Open App.Path & "\easy\mission" & missionnum & ".txt" For Input As #1
Input #1, mission, level, description, goals
Close #1
next I
|
This will run the loop 100 times and open the random mission file to edit. |
|
| Back to top |
|
| |
nooc Master Poster

Joined: 10 Aug 2005 Posts: 215
|
Posted: Thu Nov 01, 2007 2:08 pm Post subject: |
|
|
Use FileSystemObject to list the folder files into a collection and then randomize a name from the collection.
If the file has to match a certain pattern then filter the filenames when inserting into the collection.
I don't know vb so I don't want to spend time writing an example. |
|
| Back to top |
|
| |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2984 Location: Earth
|
|
| Back to top |
|
| |
Necromis Lifeless on my Boat

Joined: 11 Apr 2005 Posts: 767 Location: USA
|
Posted: Fri Nov 02, 2007 6:12 am Post subject: |
|
|
| Just curious, which way, select/case, or the for/next loop? |
|
| Back to top |
|
| |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2984 Location: Earth
|
Posted: Fri Nov 02, 2007 7:19 am Post subject: |
|
|
I edited your example, changing it slightly so that it didn't use select, i used if and checked against some buttons. my final code was like so
| Code: | If opteasy.Value = True Then
Randomize
missionnum = (Int(((txteasyfiles.Text + 1) - 1) * Rnd) + 1)
Open App.Path & "\easy\mission" & missionnum & ".txt" For Input As #1
End If
If optmedium.Value = True Then
Randomize
missionnum = (Int(((txtmediumfiles.Text + 1) - 1) * Rnd) + 1)
Open App.Path & "\medium\mission" & missionnum & ".txt" For Input As #1
End If
If opthard.Value = True Then
Randomize
missionnum = (Int(((txthardfiles.Text + 1) - 1) * Rnd) + 1)
Open App.Path & "\hard\mission" & missionnum & ".txt" For Input As #1
End If |
_________________ Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG
Caretaker - A Star Trek Voyager TCG |
|
| Back to top |
|
| |
Necromis Lifeless on my Boat

Joined: 11 Apr 2005 Posts: 767 Location: USA
|
Posted: Fri Nov 02, 2007 9:02 am Post subject: |
|
|
| I think there might be another way of doing this that might be a bit cleaner. I remember reading it in one of the VB.net books I learned from. It basically would take the value of the option box for you. You could then just have one invoked calling, rather than multiple if/thens. Let me research this and get back to you on it. I am assuming this is an On_click of a command button, right? |
|
| Back to top |
|
| |
Necromis Lifeless on my Boat

Joined: 11 Apr 2005 Posts: 767 Location: USA
|
Posted: Fri Nov 02, 2007 9:16 am Post subject: |
|
|
Ok here is a snippet of the text from where I saw it. I think this might help you make your code cleaner. It is VB.net, but It could work in vb6. Note a piece missing from this text indicates to make the | Code: | Dim ticketClass As String
|
part of the forms class, and not in the actual event.
| Quote: |
Click the first radio button, and then using the Properties window, click the lightning bolt to see the list of events for the control. Click in the entry area next to the CheckedChanged property, just once. In other examples, you would have double-clicked this to automatically drop into the code window with an event handler created and named for you by VB 2005 Express. This time around, you're going to give the event handler your own name. Type in TicketTypeChanged.
When you press the Enter key, the code editor will open. As you've probably guessed from the name, this event fires whenever the Checked property of a radio button changes. The important thing to note is that this event fires both when a radio button is selected and when it's deselected—it's not like a Click event on a button, which would fire only when the user explicitly pointed at the control with the mouse and clicked it.
Now, don't bother writing any code into the event handler just yet. Instead, head on back to the designer view and select the next radio button. This time, in the event list click the drop-down arrow beside the CheckedChanged event and select the event handler you just created. Repeat the process for the remaining radio buttons on the form and you'll end up with them all having the same event handler for their CheckedChanged event.
When you've set the radio buttons all up, press F7 to drop into the code window once again.
Take a look at the event handler method you created:
Private Sub TicketTypeChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles economyRadio.CheckedChanged, upperRadio.CheckedChanged, _
premiumRadio.CheckedChanged, firstRadio.CheckedChanged, _
businessRadio.CheckedChanged
End Sub
Notice that you get two parameters passed to the event. The first one is really useful because it is the control that fired the event. You can use this to figure out just why the event fired (was the control deselected or selected). More to the point, you can pull the text out of the control's Text property and use that to set up your ticketClass member variable.
Add code that looks like this to the event handler:
Private Sub TicketTypeChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles economyRadio.CheckedChanged, upperRadio.CheckedChanged, _
premiumRadio.CheckedChanged, firstRadio.CheckedChanged, _
businessRadio.CheckedChanged
Dim ticketTypeButton As RadioButton = CType(sender, RadioButton)
If ticketTypeButton.Checked Then
ticketClass = ticketTypeButton.Text
End If
End Sub
The first thing this new code does is cast the sender parameter to a RadioButton; you know that it will work because this code gets called only in response to a RadioButton on the form calling it.
After that's done, you can just take a look at the Checked property to see if the event fired because the sender control got selected. If it did, the contents of its Text property are copied into your member variable.
|
|
|
| Back to top |
|
| |
|
|
|