Lifelesspeople.com

 Forum FAQsForum FAQs  Knowledge BaseKnowledge Base  RulesRules   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   HostingHosting   RegisterRegister 
 DonateDonate   WikiWiki   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

vb6 randomize files

 
Lifelesspeople.com Forum Index -> Web Architects' Abode
Post new topic   Reply to topic View previous topic :: View next topic  
Author Message
Desbrina
Jadeite


Joined: 11 Jun 2005
Posts: 2984
Location: Earth

PostPosted: Thu Nov 01, 2007 10:29 am    Post subject: vb6 randomize files Reply with quote

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

PostPosted: Thu Nov 01, 2007 12:22 pm    Post subject: Reply with quote

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


PostPosted: Thu Nov 01, 2007 2:08 pm    Post subject: Reply with quote

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

PostPosted: Fri Nov 02, 2007 4:12 am    Post subject: Reply with quote

I've done it the way Necromis suggested and it works fine. Thanks for the help
_________________
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

PostPosted: Fri Nov 02, 2007 6:12 am    Post subject: Reply with quote

Just curious, which way, select/case, or the for/next loop?
Back to top
 
Desbrina
Jadeite


Joined: 11 Jun 2005
Posts: 2984
Location: Earth

PostPosted: Fri Nov 02, 2007 7:19 am    Post subject: Reply with quote

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

PostPosted: Fri Nov 02, 2007 9:02 am    Post subject: Reply with quote

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

PostPosted: Fri Nov 02, 2007 9:16 am    Post subject: Reply with quote

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
 
Display posts from previous:   
Post new topic   Reply to topic    Lifelesspeople.com Forum Index -> Web Architects' Abode All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Home | Hosting | News | Forum | Links | System Status | About | Archive | Donate ]
Powered by phpBB © 2001, 2002 phpBB Group
All trademarks and copyrights on this page are owned by their respective owners. Posts and comments are owned by the poster. Everything else © 2001 - 2007 Lifelesspeople.com