|
-
Aug 17th, 2009, 02:13 AM
#1
Thread Starter
Member
Helping with this topic
Here is what it looks like 
this program simply asked the user to click flag and a random number will be generated from 0 to 11 which represents the countries. I have my flags located in a folder "Projects\Flags\flags\China.gif" for example. Then the picture will be displayed in the picture box. Then the user will have to click on the correct country radio button(group into a panel). If the user click the correct country, a message will be displayed.
1. My question here is, how can i assign the country flags to a variable in sequence from 0 to 11, so that i could call it upon random number generation?
2. How can i later display the country flag in the picture box (from the location of the flags picture)?
Note : This is just a sample picture, so the number of countries varies from what i ma doing.
-
Aug 17th, 2009, 07:05 AM
#2
Re: Helping with this topic
use an array. either a string array which holds the full path & filename for each image, or, an array of bitmaps which contains the actual bitmaps.
in your form_load event, use io.directory.getfiles to populate your array.
if you post your code i might be able to give you a more specific answer
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 17th, 2009, 08:18 AM
#3
Thread Starter
Member
Re: Helping with this topic
Code:
Public Class Form1
Private Function RandomNumber(ByVal min As Integer, ByVal max As Integer) As Integer
Dim random As New Random()
Return random.Next(min, max)
End Function 'RandomNumber function returns a random number when called
Private Sub btnFlag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlag.Click
Dim random As Integer
random = RandomNumber(0, 11) 'generate a random number from 0 to 11
Dim countryPic(12) As String 'create an array
countryPic(0) = "Flags\bin\Debug\Afghanistan.gif" 'assigning picture's location
countryPic(1) = "Flags\bin\Debug\Bangladesh.gif"
countryPic(2) = "Flags\bin\Debug\Belgium.gif"
countryPic(3) = "Flags\bin\Debug\Canada.gif"
countryPic(4) = "Flags\bin\Debug\China.gif"
countryPic(5) = "Flags\bin\Debug\Egypt.gif"
countryPic(6) = "Flags\bin\Debug\Finland.gif"
countryPic(7) = "Flags\bin\Debug\France.gif"
countryPic(8) = "Flags\bin\Debug\Greenland.gif"
countryPic(9) = "Flags\bin\Debug\Malaysia.gif"
countryPic(10) = "Flags\bin\Debug\Sudan.gif"
countryPic(11) = "Flags\bin\Debug\Zambia.gif"
Dim i As Integer = random
IO.Directory.GetFiles(countryPic(1))
End Sub
Private Sub picFlag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picFlag.Click
End Sub
End Class
I cannot get the IO.Directory.GetFiles(countryPic(1)) to work.
NOTE: I used 1 as to test only, but later going to use i.
And my directory is not correct too. My full path is "C:\Documents and Settings\myname\My Documents\Visual Studio 2008\Projects\Flags\Flags\bin\Debug\Bangladesh.gif" . What should i put as the relative path instead of "Flags\bin\Debug\Bangladesh.gif" as this doesn't work.
Last edited by sanox; Aug 17th, 2009 at 08:22 AM.
-
Aug 17th, 2009, 08:25 AM
#4
Thread Starter
Member
Re: Helping with this topic
Sorry, my browser is making problems
Last edited by sanox; Aug 17th, 2009 at 08:32 AM.
-
Aug 17th, 2009, 08:25 AM
#5
Fanatic Member
Re: Helping with this topic
Check out this CodeBank submision: 'Unique, Random Selection from a List'.
Hope that helps .
-
Aug 17th, 2009, 08:30 AM
#6
Re: Helping with this topic
as the images are in your applications executablepath you don't need to specify a full path.
vb Code:
Private Sub btnFlag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlag.Click
Dim random As Integer
random = RandomNumber(0, 12) 'generate a random number from 0 to 11
Dim countryPic(12) As String 'create an array
countryPic(0) = "Afghanistan.gif" 'assigning picture's location
countryPic(1) = "Bangladesh.gif"
countryPic(2) = "Belgium.gif"
countryPic(3) = "Canada.gif"
countryPic(4) = "China.gif"
countryPic(5) = "Egypt.gif"
countryPic(6) = "Finland.gif"
countryPic(7) = "France.gif"
countryPic(8) = "Greenland.gif"
countryPic(9) = "Malaysia.gif"
countryPic(10) = "Sudan.gif"
countryPic(11) = "Zambia.gif"
picturebox1.imagelocation = countryPic(random)
'picturebox1.tag = ?put correct answer in the .tag property
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 17th, 2009, 08:30 AM
#7
Thread Starter
Member
Re: Helping with this topic
sorry, my browser is making problems
-
Aug 17th, 2009, 08:59 AM
#8
Thread Starter
Member
Re: Helping with this topic
Thanks guys, but my problem now is my picture does not fit the picturebox. How to reframe the picture to fit the picture box?
-
Aug 17th, 2009, 09:05 AM
#9
Thread Starter
Member
Re: Helping with this topic
-
Aug 17th, 2009, 09:57 AM
#10
Thread Starter
Member
Re: Helping with this topic
Code:
Private Sub pnlCountries_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pnlCountries.Paint
If rdbAfghanistan.Checked And random = 0 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbBangladesh.Checked And random = 1 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbBelgium.Checked And random = 2 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbCanada.Checked And random = 3 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbChina.Checked And random = 4 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbEgypt.Checked And random = 5 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbFinland.Checked And random = 6 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbFrance.Checked And random = 7 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbGreenland.Checked And random = 8 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbMalaysia.Checked And random = 9 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbSudan.Checked And random = 10 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
If rdbZambia.Checked And random = 11 Then
MessageBox.Show("you are right")
Else
MessageBox.Show("you are wrong")
End If
End Sub
How do do this in a simple way? I mean like. Using FOR LOOP instead of those, Can radio button in a panel be assigned to a sequence of numbers? If can, how can i accomplish that as i want to use FOR loop in the "panal private sub"
-
Aug 17th, 2009, 10:46 PM
#11
Thread Starter
Member
Re: Helping with this topic
-
Aug 17th, 2009, 11:14 PM
#12
Re: Helping with this topic
Code:
Public Class Form1
Dim flags As List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.flags = New List(Of String)
Dim dInfo As New IO.DirectoryInfo("path to the images' directory")
'Get all the files in the image directory.
For Each fInfo As IO.FileInfo In dInfo.GetFiles(".gif")
flags.Add(fInfo.FullName)
Next
Me.FlagPictureBox.BackgroundImageLayout = ImageLayout.Zoom
End Sub
Private Sub FlagButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlagButton.Click
Dim rand As New Random
Dim index As Integer = rand.Next(0, 10)
Me.FlagPictureBox.BackgroundImage = Image.FromFile(flags(index))
Me.FlagPictureBox.Tag = IO.Path.GetFileNameWithoutExtension(flags(index))
End Sub
Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles BelgiumRadioButton.CheckedChanged, _
ChinaRadioButton.CheckedChanged, _
CanadaRadioButton.CheckedChanged
Dim rButton As RadioButton = DirectCast(sender, RadioButton)
If rButton.Text.ToUpperInvariant = CStr(Me.FlagPictureBox.Tag).ToUpperInvariant Then
MessageBox.Show("You are right")
Else
MessageBox.Show("You are wrong")
End If
End Sub
End Class
Last edited by VBDT; Aug 17th, 2009 at 11:23 PM.
-
Aug 17th, 2009, 11:21 PM
#13
Re: Helping with this topic
 Originally Posted by sanox
Thanks guys, but my problem now is my picture does not fit the picturebox. How to reframe the picture to fit the picture box?
Use the pictureBox's Backgroundimag property instead of Imag and set the picturBox's "BackgroundImageLayout" to Zoom.
-
Aug 17th, 2009, 11:39 PM
#14
Re: Helping with this topic
 Originally Posted by VBDT
Use the pictureBox's Backgroundimag property instead of Imag and set the picturBox's "BackgroundImageLayout" to Zoom.
Use the Image property, which is what the PictureBox is for, and set the SizeMode to Zoom.
-
Aug 17th, 2009, 11:47 PM
#15
Re: Helping with this topic
 Originally Posted by jmcilhinney
Use the Image property, which is what the PictureBox is for, and set the SizeMode to Zoom.
That’s a good catch
-
Aug 18th, 2009, 01:26 AM
#16
Thread Starter
Member
Re: Helping with this topic
Thanks guys, i am almost done. I have probably one last problem dealing with string concatenation.
Code:
'Dim choppedCountryName As String
Dim countryPic(12) As String
countryPic(0) = "Afghanistan.gif"
If rdbAfghanistan.Checked And random = 0 Then
lblMessage.Text = "Congratulations, you'ge got the corrent one"
Else
lblMessage.Text = "Sorry, you are wrong, the correct answer is " '& choppedCountryName = countryPic(random).Remove(0, countryPic(random).Length - 4)
End If
I would like to delete the 4 last characters (".gif"), how can i accomplish that?
-
Aug 18th, 2009, 01:48 AM
#17
Re: Helping with this topic
 Originally Posted by sanox
Thanks guys, i am almost done. I have probably one last problem dealing with string concatenation.
Code:
'Dim choppedCountryName As String
Dim countryPic(12) As String
countryPic(0) = "Afghanistan.gif"
If rdbAfghanistan.Checked And random = 0 Then
lblMessage.Text = "Congratulations, you'ge got the corrent one"
Else
lblMessage.Text = "Sorry, you are wrong, the correct answer is " '& choppedCountryName = countryPic(random).Remove(0, countryPic(random).Length - 4)
End If
I would like to delete the 4 last characters (".gif"), how can i accomplish that?
This question has nothing to do with the topic of this thread and belongs in a thread of its own. Please keep each thread to a single topic and each topic to a single thread.
You can use the Path class from the System.IO namespace to manipulate file and folder paths. It has a GetFileNameWithoutExtension method for getting only the file name, excluding extension, from a path.
-
Aug 18th, 2009, 01:55 AM
#18
Thread Starter
Member
Re: Helping with this topic
This is too advanced.. I cant find the GetFileNameWithoutExtension when i type "getfile". The GetFileNameWithoutExtension does not appear
-
Aug 18th, 2009, 02:13 AM
#19
Re: Helping with this topic
When you type it where? It doesn't appear where? Maybe you should enter the full name wherever it is you're only typing "getfile" at the moment.
-
Aug 18th, 2009, 07:29 AM
#20
Thread Starter
Member
Re: Helping with this topic
Normally, the properties and thee label names are auto complete when we tab or it will show(selections) while we are typing.
It show "GetFileNameWithoutExtension is not declared"
I think it would be easier if i could delete the last 4 letters from the array -- countryPic(0) = "Afghanistan.gif"
I tried this
Code:
Public Shared Function GetFileNameWithoutExtension(ByVal path As String) As String
End Function
lblMessage.Text = "Sorry, you are wrong, the correct answer is " & GetFileNameWithoutExtension(countryPic(random))
It did not work also
Last edited by sanox; Aug 18th, 2009 at 08:24 AM.
-
Aug 18th, 2009, 08:48 AM
#21
Thread Starter
Member
Re: Helping with this topic
Help Please. I just want to delete the 4 last characters. That's all, since the above function does not work.
-
Aug 18th, 2009, 09:15 AM
#22
Re: Helping with this topic
GetFileNameWithoutExtension is, as I said, a member of the System.IO.Path class. If you don't know how to use it then look it up in the Help.
-
Aug 18th, 2009, 10:19 AM
#23
Re: Helping with this topic
vb Code:
dim filename as string = "Afghanistan.gif"
filename = io.path.GetFileNameWithoutExtension(filename)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
|