 |
 |

04-17-2009, 12:19 PM
|
 |
Freshman
|
|
Join Date: Apr 2009
Posts: 26
|
|
Random File Chooser
|
I am a total noob to VB and am only doing an A-level in computing. i'm using VB 2008 express edition
i am trying to make a windows form application which picks a random file from a folder and displays its contents in text boxes when a button is clicked. i have tried everything my feeble A-level mind can Think of and need some help.
Any suggestions?
|
|

04-17-2009, 12:43 PM
|
 |
Senior Contributor
|
|
Join Date: Jun 2003
Location: Birmingham, England, UK
Posts: 821
|
|
Firstly, this is in the wrong forum... you need to post in .NET although I imagine this will be moved for you...
You have 3 problems:
1) Enumerate files in a directory
2) Pick one at random (by generating a random number between 1 and the number of files found in the directory)
3) Read the file into a string and print into a textbox.
I recommend you tackle these problems in the order I've stated above. For problem 1, something along the lines of...
Code:
Dim ioDirectory As New IO.DirectoryInfo("c:\path\to\your\directory")
Dim ioFiles As IO.FileInfo() = Directory.GetFiles("*.*") 'or maybe *.txt for text files
Dim ioFile As IO.FileInfo
For Each ioFile In ioFiles
MsgBox(ioFile.Name)
Next
... should get you started.
|
|

04-17-2009, 12:56 PM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
|
|
First. Welcome to the forum. Don't forget to read the forum Posting Guidelines.
Use the System.IO.Directory and System.Random objects.
Code:
Public Class Form1
Dim myFiles() As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Run this once.
myFiles = System.IO.Directory.GetFiles("C:\", "*.*") ' Apply your own folder path
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim R As New System.Random
Dim index As Integer
' Point to different elements of the myfiles array
index = R.Next(myFiles.Length - 1)
MessageBox.Show(myFiles(index))
End Sub
End Class
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
|

04-17-2009, 01:59 PM
|
 |
Freshman
|
|
Join Date: Apr 2009
Posts: 26
|
|
Cheersen
|
Cheers Gruff
it seems to work fine if i just put the button 1 code in the button 2 sub, why did you do that in the first place? (trying to broaden my knowledge here not just randomly questioning you lol)
|
|

04-17-2009, 03:31 PM
|
 |
Impetuous & volatile
* Expert *
|
|
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
|
|
Quote:
Originally Posted by Jazman667
Cheers Gruff
it seems to work fine if i just put the button 1 code in the button 2 sub, why did you do that in the first place? (trying to broaden my knowledge here not just randomly questioning you lol)
|
Gruff seperated the functionality of gathering information about which files was located in the specified folder and the actually randomly selecting one among the found files. If you think about it, it should make perfectly good sense performance wise assuming that no new files are getting added to the folder.
Imagine having a folder with 1 million files. If you had to create a list of all the files everytime you wanted to select a random file, then you'd have to - Create a new list
- Iterate all 1 million files adding them to the list
- Select random file from list
Instead, if you just maintained the list of files, then you could simply keep selecting random files from the list instead of having to put together the list first. The only disadvantage is, that if files get added to the folder after you put together the initial list, then you'll have to recreate the list (or expand it) to contain the new files as well.
|
__________________
Reading is the foundation for all knowledge - Unknown.
Last edited by Qua; 04-17-2009 at 03:36 PM.
|

04-18-2009, 12:42 PM
|
 |
Freshman
|
|
Join Date: Apr 2009
Posts: 26
|
|
:)
|
Ok that makes sense i've got a tab control thats used to get to the page where the file is chosen. would it be a good idea to put this line into the click action of the tab page?
|
|

04-19-2009, 10:26 AM
|
 |
Impetuous & volatile
* Expert *
|
|
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
|
|
It all depends on which logic you want to use. If you want to iterate and list the files everytime the tab page is clicked, then you should execute the code there 
|
__________________
Reading is the foundation for all knowledge - Unknown.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|