Results 1 to 8 of 8

Thread: Help me please

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    5

    Exclamation Help me please

    Hi

    It is the first time for me to participate in this site.
    At the same time, i'm new with vb.net and i'm facing a problem.
    I'm trying to choose a picture in a pictureBox randomly(a picture from a group of pictures). Please help me to do the coding.

    Thanks.

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Help me please

    Where are the files for the group of pictures located?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Help me please

    Hi Yum Yum !!
    Welcome on the board!!!

    Here a sample of what you are looking for

    VB Code:
    1. Dim Files() As String
    2. Dim rnd As New Random
    3.  
    4. Files = IO.Directory.GetFiles("C:\ImgLib\Big Jpeg")
    5. Me.PictureBox1.Image = New Bitmap(Files(rnd.Next(Files.Length)))
    Last edited by Zakary; Jun 17th, 2005 at 12:37 PM.
    Using VS 2010 on Fw4.0

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    5

    Re: Help me please

    Thanks alot guys;

    But;
    can i follow the same way if i have a botton called flag for example, and when i press it, a flag of group of flags located in bin folder is randomly displayed in a picture box ?

  5. #5
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Help me please

    Quote Originally Posted by YumYum
    Thanks alot guys;

    But;
    can i follow the same way if i have a botton called flag for example, and when i press it, a flag of group of flags located in bin folder is randomly displayed in a picture box ?
    Hein !?!? I do not understand your request, please try again
    Using VS 2010 on Fw4.0

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

    Re: Help me please

    Quote Originally Posted by YumYum
    Thanks alot guys;

    But;
    can i follow the same way if i have a botton called flag for example, and when i press it, a flag of group of flags located in bin folder is randomly displayed in a picture box ?
    That's exactly what Zakary's code does. You simply substitute Application.StartupPath for the path he provided and also provide a second argument to GetFiles like "*.jpg" to get just the JPG files. Then put the code in the Click event handler of your button.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    5

    Re: Help me please

    Thanks alot brothers: jmcilhinney,Zakary and Webtest

    I did that one, and it worked. Now i have another problem; i have numbers form 10 to 99 and i want to select only 10 of them in random and display them in a listBox. Would you help me please !
    Should i use any loop ?

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

    Re: Help me please

    Yes you should.
    VB Code:
    1. Dim rand As New Random
    2.         Dim item As Integer
    3.  
    4.         While Me.ListBox1.Items.Count < 10
    5.             item = rand.Next(10, 100)
    6.  
    7.             If Not Me.ListBox1.Items.Contains(item) Then
    8.                 Me.ListBox1.Items.Add(item)
    9.             End If
    10.         End While
    Note that I have used a While loop in case the ListBox already has 10 items. If you know that the ListBox is empty to start with, it would be more correct to use Do...Until Me.ListBox1.Items.Count = 10. The If statement is used to ensure that no duplicates are added to the list. If duplicates are allowed you can omit the If statement. In fact, if duplicates are allowed and you know the list starts empty, you would just use a For loop, but I'm guessing that duplicates are not allowed. Also note that, if you aren't already aware, Random.Next returns numbers in this range: 10 <= Random.Next < 100. That is why I used 10 and 100 instead of 10 and 99.

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