Question Image Resource Not loading in PictureBox1

scotbuff

Member
Joined
Jun 9, 2010
Messages
5
Programming Experience
Beginner
Being new to VB.net I am sure I am doing to something really stupid here. But I cannot seem to get my image resources to load into PictureBox1. I can see all the images I added as resources in the solution explorer. They are a series of PNG files with names associated with countries. I am at the beginning stages of randomly changing the image that is displayed in PictureBox1, but I cannot even get the first image to load. Here is my code so far. When I click on Button2 I can see that my array returns a random country, but no image loads. I even tried typing in several of the countries names as well as the country name followed by png (e.g. chine.png) and again no image loads. What am I doing wrong? Thank you.

VB.NET:
Public Class Form1
   Dim randomObject As New Random()
   Dim Flags() As String = {"australia", "brazil", "china", "italy", "russia", "south africa", "spain"}
   Dim flagResult1 As Integer
   Dim flagResult2 As Integer
   Dim flagResult3 As Integer
   Dim flagResult4 As Integer
   Dim flagResult5 As Integer

   Public Function randomFlag() As Integer
      Dim flagIndex = randomObject.Next(0, 7)
      Return flagIndex
   End Function

   Sub ChooseFlag(ByVal flagImage As PictureBox)
      Dim pictureResource As Image
      Dim Country As String
      flagResult1 = randomFlag()
      Country = Flags(flagResult1)
      With Label1
         .Text = Country
      End With
      
      pictureResource = My.Resources.ResourceManager.GetObject(Country)
      PictureBox1.Image = CType(pictureResource, Image)
   End Sub


   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      ChooseFlag(PictureBox1)
      Button2.Enabled = False
   End Sub
End Class
 
Thank you

I must not have added the resources correctly. I thought I did it just like that, but apparently I was mistaken. I got the code to work correctly now. Thank you, you guys are great.
 
Back
Top