|
-
Jun 17th, 2005, 11:53 AM
#1
Thread Starter
New Member
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.
-
Jun 17th, 2005, 12:18 PM
#2
Frenzied Member
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
-
Jun 17th, 2005, 12:31 PM
#3
Re: Help me please
Hi Yum Yum !!
Welcome on the board!!!
Here a sample of what you are looking for
VB Code:
Dim Files() As String
Dim rnd As New Random
Files = IO.Directory.GetFiles("C:\ImgLib\Big Jpeg")
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
-
Jun 18th, 2005, 07:08 AM
#4
Thread Starter
New Member
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 ?
-
Jun 18th, 2005, 08:56 AM
#5
Re: Help me please
 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
-
Jun 18th, 2005, 01:33 PM
#6
Re: Help me please
 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.
-
Jun 19th, 2005, 03:53 PM
#7
Thread Starter
New Member
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 ?
-
Jun 19th, 2005, 08:04 PM
#8
Re: Help me please
Yes you should.
VB Code:
Dim rand As New Random
Dim item As Integer
While Me.ListBox1.Items.Count < 10
item = rand.Next(10, 100)
If Not Me.ListBox1.Items.Contains(item) Then
Me.ListBox1.Items.Add(item)
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|