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
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.
Re: Helping with this topic
Sorry, my browser is making problems
Re: Helping with this topic
Check out this CodeBank submision: 'Unique, Random Selection from a List'.
Hope that helps ;).
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
Re: Helping with this topic
sorry, my browser is making problems
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?
Re: Helping with this topic
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"
Re: Helping with this topic
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
Re: Helping with this topic
Quote:
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.
Re: Helping with this topic
Quote:
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.
Re: Helping with this topic
Quote:
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 :thumb:
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?
Re: Helping with this topic
Quote:
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.
Re: Helping with this topic
This is too advanced.. I cant find the GetFileNameWithoutExtension when i type "getfile". The GetFileNameWithoutExtension does not appear
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.
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
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.
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.
Re: Helping with this topic
vb Code:
dim filename as string = "Afghanistan.gif"
filename = io.path.GetFileNameWithoutExtension(filename)