Results 1 to 6 of 6

Thread: Randomly select Treeview Items

  1. #1

    Thread Starter
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    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:
    1. Private Sub RandomAllGenie()
    2.         Dim RandGen As New Random
    3.         Dim GenStr As String
    4.  
    5.         Randomize()
    6.         Dim RandNum As Integer = RandGen.Next(1, (NumFiles + 1))
    7.         GenStr = DirName & "\" & GenArr(RandNum)
    8.         picRealGenie.Image = Image.FromFile(GenStr)
    9.  
    10.         picPreview.Image = Image.FromFile(GenStr)
    11.         picPreview.Refresh()
    12.         ApplyGenie()
    13.     End Sub
    14.  
    15.    Private Sub frmGenie_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    16.         TreeGenie()
    17.         LoadReg()
    18.         RandomAllGenie()
    19.        
    20.     End Sub
    21.  
    22.   Private Sub TreeGenie()
    23.         Dim FolderNode As TreeNode
    24.         Dim FilePath As String
    25.         Dim FolderPath As String
    26.         Dim Xtension As String
    27.         Dim diNext As DirectoryInfo
    28.  
    29.         For Each FolderPath In Directory.GetDirectories(DirName, "*")
    30.             FolderNode = trGenie.Nodes.Add(Path.GetFileName(FolderPath))
    31.  
    32.             For Each FilePath In Directory.GetFiles(FolderPath)
    33.                 Xtension = IO.Path.GetExtension(FilePath)
    34.  
    35.                 If Xtension = ".bmp" OrElse Xtension = ".jpg" OrElse Xtension = ".JPG" OrElse Xtension = ".jpe" OrElse Xtension = ".gif" Then
    36.                     FolderNode.Nodes.Add(Path.GetFileName(FilePath))
    37.                     NumFiles = NumFiles + 1
    38.                     If AutoGenieReg = "Automatic" Then
    39.                         GenArr(NumFiles) = Path.GetFileName(FilePath)
    40.                     End If
    41.                 End If
    42.             Next
    43.         Next
    44.         lblNumFiles.Text = NumFiles.ToString() & " Items In Repository"
    45.     End Sub

    Any help would be GreaTlY appreciated
    VB.NET MVP 2008 - Present

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Randomly select Treeview Items

    So whats the problem?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Randomly select Treeview Items

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Randomly select Treeview Items

    Quote 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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width