Results 1 to 23 of 23

Thread: Helping with this topic

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    Re: Helping with this topic

    Sorry, my browser is making problems
    Last edited by sanox; Aug 17th, 2009 at 08:32 AM.

  5. #5
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Helping with this topic

    Check out this CodeBank submision: 'Unique, Random Selection from a List'.

    Hope that helps .
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Helping with this topic

    as the images are in your applications executablepath you don't need to specify a full path.

    vb Code:
    1. Private Sub btnFlag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlag.Click
    2.     Dim random As Integer
    3.     random = RandomNumber(0, 12) 'generate a random number from 0 to 11
    4.     Dim countryPic(12) As String 'create an array
    5.     countryPic(0) = "Afghanistan.gif" 'assigning picture's location
    6.     countryPic(1) = "Bangladesh.gif"
    7.     countryPic(2) = "Belgium.gif"
    8.     countryPic(3) = "Canada.gif"
    9.     countryPic(4) = "China.gif"
    10.     countryPic(5) = "Egypt.gif"
    11.     countryPic(6) = "Finland.gif"
    12.     countryPic(7) = "France.gif"
    13.     countryPic(8) = "Greenland.gif"
    14.     countryPic(9) = "Malaysia.gif"
    15.     countryPic(10) = "Sudan.gif"
    16.     countryPic(11) = "Zambia.gif"
    17.     picturebox1.imagelocation = countryPic(random)
    18.     'picturebox1.tag = ?put correct answer in the .tag property
    19.  
    20. End Sub

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    Re: Helping with this topic

    sorry, my browser is making problems

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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?

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    Re: Helping with this topic

    Sorry. Browser problem

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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"

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    Re: Helping with this topic

    Please help please.

  12. #12
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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

  13. #13
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Helping with this topic

    Quote Originally Posted by sanox View Post
    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.

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

    Re: Helping with this topic

    Quote Originally Posted by VBDT View Post
    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.
    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

  15. #15
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Helping with this topic

    Quote Originally Posted by jmcilhinney View Post
    Use the Image property, which is what the PictureBox is for, and set the SizeMode to Zoom.
    That’s a good catch

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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?

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

    Re: Helping with this topic

    Quote Originally Posted by sanox View Post
    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.
    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

  18. #18

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    Re: Helping with this topic

    This is too advanced.. I cant find the GetFileNameWithoutExtension when i type "getfile". The GetFileNameWithoutExtension does not appear

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

    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.
    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

  20. #20

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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.

  21. #21

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    49

    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.

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

    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.
    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

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Helping with this topic

    vb Code:
    1. dim filename as string = "Afghanistan.gif"
    2. filename = io.path.GetFileNameWithoutExtension(filename)

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