|
-
Aug 30th, 2005, 03:51 AM
#1
Randomly select Treeview Items
Hello!
It's me again...
In my app I have a sub named RandomAllGenie which is supposed to select or identify a childnode from a treeview. based on that, it needs to set a picture accordingly in the picturebox.
How can I achieve that ¿
VB Code:
Private Sub RandomAllGenie()
Dim RandGen As New Random
Dim GenStr As String
Randomize()
Dim RandNum As Integer = RandGen.Next(1, (NumFiles + 1))
GenStr = DirName & "\" & GenArr(RandNum)
picRealGenie.Image = Image.FromFile(GenStr)
picPreview.Image = Image.FromFile(GenStr)
picPreview.Refresh()
ApplyGenie()
End Sub
Private Sub frmGenie_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
TreeGenie()
LoadReg()
RandomAllGenie()
End Sub
Private Sub TreeGenie()
Dim FolderNode As TreeNode
Dim FilePath As String
Dim FolderPath As String
Dim Xtension As String
Dim diNext As DirectoryInfo
For Each FolderPath In Directory.GetDirectories(DirName, "*")
FolderNode = trGenie.Nodes.Add(Path.GetFileName(FolderPath))
For Each FilePath In Directory.GetFiles(FolderPath)
Xtension = IO.Path.GetExtension(FilePath)
If Xtension = ".bmp" OrElse Xtension = ".jpg" OrElse Xtension = ".JPG" OrElse Xtension = ".jpe" OrElse Xtension = ".gif" Then
FolderNode.Nodes.Add(Path.GetFileName(FilePath))
NumFiles = NumFiles + 1
If AutoGenieReg = "Automatic" Then
GenArr(NumFiles) = Path.GetFileName(FilePath)
End If
End If
Next
Next
lblNumFiles.Text = NumFiles.ToString() & " Items In Repository"
End Sub
Any help would be GreaTlY appreciated
VB.NET MVP 2008 - Present
-
Aug 30th, 2005, 04:19 AM
#2
Re: Randomly select Treeview Items
-
Aug 30th, 2005, 04:21 AM
#3
Re: Randomly select Treeview Items
You're getting mixed up between the System.Random class and the Runtime method Randomize. They each have NOTHING to do with the other. Randomize is intended to be used in conjunction with the Rnd Runtime function, but I'd suggest you stick with the Random class. You should create one Random object rather than a new one each time you call the function. Create a class level variable like this:
VB Code:
Private RandGen As New Random(CInt(Date.Now.Ticks Mod Integer.MaxValue))
By seeding the Random with a time based value you should get a different psuedo-random sequence each time. If you use no seed or the same seed each time then the same psuedo-random sequence will be generated every time. Now that you have a Random object, you just call Next each time you need random number. You would call it like this:
VB Code:
myRandomNumber = Me.RandGen.Next(0, Me.GenArr.Length)
Note that the value returned will be >= to the minimum specified (zero) and < than the maximum (the length of the array). You would substitute Count for Length if GenArr is a collection, but the name suggests an array.
-
Aug 30th, 2005, 04:31 AM
#4
Re: Randomly select Treeview Items
Its not actually necessary to manually seed the Random class, the constructor does that itself based on the system time anyway. See MSDN.
I don't live here any more.
-
Aug 30th, 2005, 04:37 AM
#5
Re: Randomly select Treeview Items
 Originally Posted by wossname
Its not actually necessary to manually seed the Random class, the constructor does that itself based on the system time anyway. See MSDN.
Well there you go. I didn't know that, but it does make sense. I even just read the relevant help topic to confirm it, not that I didn't have faith of course.
-
Aug 30th, 2005, 04:39 AM
#6
Re: Randomly select Treeview Items
 Originally Posted by jmcilhinney
If you use no seed... each time then the same psuedo-random sequence will be generated every time.
Who's spouting that rubbish?
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
|