How to get an image from my.resources by string name

TALSOFT

Active member
Joined
Feb 23, 2012
Messages
34
Programming Experience
5-10
So I am wondering what I am doing wrong here
I am trying to load a GIF file by its string name from my.resources as what GIF to show is chosen at random
and based off that random number we get a valid string name. The names come out proper because I used the Msgbox trick

Here is my faulty coding
Public Function PrintCardName(ByVal Suit As String, ByVal Hand As String, ByRef ImgBoxUpdate As System.Windows.Forms.PictureBox)
'Working code omitted to save reading time
ImgBoxUpdate.Image = My.Resources.ResourceManager.GetObject(Card_File_Name)
End Function
And Card_File_Name is a predefined string, and no there is no space after the letter N in card_file_name it just appears that way because its italicized
The issue is that the Imagebox that is referanced to IMGBOXUPDATE goes blank after this function.
 
Last edited:

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
117
Location
UK
Programming Experience
1-3
well to get an image for single use it's quite simple

Code:
logo.Image = My.Resources.Button
"Button" being the image, Really you should have used an ImageList Control and Randomized that as if you have any other resources like an "Icon,Executable file, TxTFile" ect.. then the picturebox will be trying to load these to which in turn will make it blank, I'm suprised it hasn't thrown an Error Exception, maybe I'm wrong...... hope this helps.
 

TALSOFT

Active member
Joined
Feb 23, 2012
Messages
34
Programming Experience
5-10
Well lets just say that the IMAGE is of a card and that card is randomly selected by a few conditions.
Then a string is made of the card suit and card number. That string is CARD_FILE_NAME and it is changing every time the user clicks a button.
So rather than code out 52 if than statements for each time the user clicks the button, why cant I just call it by the created string.
If I were to use a msgbox on Card_File_Name the output would be one of 52 valid names of images within the my.resources
for example msgbox(card_file_name) might return "h3.gif" whereas h is determined by the random suit picked and 3 is determined by the random card chosen out of 13 in each suit.

PS thanks for the speedy reply
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
117
Location
UK
Programming Experience
1-3
hi firstly i wouldn't code out 52 If Then statements my self, Quite a Silly Idea, Secondly i get what your trying to achive, so ill try and help.

No. 1 are the images named h3.gif and so on or just 3.gif?
No. 2 As i mentioned before ImageControl you can still use the same code you using just change for the imagecontrol, maybe it would be slightly easier as all the images would be stored in their, like resources but alot easier to access and program for.

hope it helps.
 

TALSOFT

Active member
Joined
Feb 23, 2012
Messages
34
Programming Experience
5-10
I appreciate the assistance.
I am working on a blackjack game.

When the user clicks the new hand button, the game selects two cards for that player this is achieved by two random statements, one for what suit of card it is i.e. Random.Next(1,4) and the other for what card it is from that suit Random.Next(1,14) , then I want it to update the image boxes with the corresponding card picture from my.resources hence the odd methood of accessing my.resources.

I know I can directly set it as
ImgBox1.Image = My.Resources.h3.gif

But that would mean that I would have to have a my.resource.CARDNAME.gif for each possible outcome. Which is why I was trying to access the resouce file by string name because its easier to deturmine what file needs to be accessed by the random outcomes. Am I making sense or should I just post a link to my source code?
 

TALSOFT

Active member
Joined
Feb 23, 2012
Messages
34
Programming Experience
5-10
BTW I completely misread what you wrote.
Yes the actual file name would be h3.gif just as an example.
As per Number 2. I will do a few google searches as I have never used imgcontrol before
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
117
Location
UK
Programming Experience
1-3
I appreciate the assistance.
I am working on a blackjack game.

When the user clicks the new hand button, the game selects two cards for that player this is achieved by two random statements, one for what suit of card it is i.e. Random.Next(1,4) and the other for what card it is from that suit Random.Next(1,14) , then I want it to update the image boxes with the corresponding card picture from my.resources hence the odd methood of accessing my.resources.

I know I can directly set it as
ImgBox1.Image = My.Resources.h3.gif

But that would mean that I would have to have a my.resource.CARDNAME.gif for each possible outcome. Which is why I was trying to access the resouce file by string name because its easier to deturmine what file needs to be accessed by the random outcomes. Am I making sense or should I just post a link to my source code?
i don't mean load the image in to the imgbox 1 at a time, Look at you Tools to the left and scroll down, you'll see a control called ImgList, it' doesn't dispaly them just holds them like the resources, but would be easier to randomize, safer too as you can add other resources which can conflict. not knowing you whole set up i'm just guessing, ImageList Control is a better option
 

TALSOFT

Active member
Joined
Feb 23, 2012
Messages
34
Programming Experience
5-10
Maybe the source LINK-VOIDED will help, keep in mind nothing is done yet, I got stuck on this issue, trying to get the imgboxes to update
 
Last edited:

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
117
Location
UK
Programming Experience
1-3
This is using ImageList just to give you and idea.

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Create a integer and new Random object
    Dim intPic As Integer
    Dim rand As New Random

    ' Pick a random number between 0 and the number of images in our imagelist control
    intPic = rand.Next(0, ImageList1.Images.Count)

    ' Now set the picturebox image equal to the image chosen from the imagelist box randomly using the random
    ' integer
    PictureBox1.Image = ImageList1.Images(intPic)
End Sub
You will need to refine this to the way you need it.
 

TALSOFT

Active member
Joined
Feb 23, 2012
Messages
34
Programming Experience
5-10
thanks for your solution, however I am not sure if it will work. For two reasons, because the cards have to be removed temporrarly when they are played from the deck of cards so that they are not called again in the same game. And also because I have other images in the resources. The approach I have taken feels right, but for whatever reason it does not work. There are other people who have utilized the santax I am using with no issues. Could it be because the imagebox cannot be shared with another class? Do I have to output the String from the Functions Class and then change the image to that string? I am going to try and see what what happens.
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
117
Location
UK
Programming Experience
1-3
Hi i haven't dissapeared, I've been looking at your code, and yes it works fine untill the "ImgBox" so i tried the msgbox(ImgBox) cut it short Change this line

Code:
FROM: Note the ImgBoxUpdate. "Image" to "InitialImage" works ok here Pictured dont dissapere, see if i'm right :)
ImgBoxUpdate.Image= My.Resources.ResourceManager.GetObject(Card_File_Name.ToString)
TO:
ImgBoxUpdate.InitialImage = My.Resources.ResourceManager.GetObject(Card_File_Name.ToString)
 

TALSOFT

Active member
Joined
Feb 23, 2012
Messages
34
Programming Experience
5-10
Hi i haven't dissapeared, I've been looking at your code, and yes it works fine untill the "ImgBox" so i tried the msgbox(ImgBox) cut it short Change this line

Code:
FROM: Note the ImgBoxUpdate. "Image" to "InitialImage" works ok here Pictured dont dissapere, see if i'm right :)
ImgBoxUpdate.Image= My.Resources.ResourceManager.GetObject(Card_File_Name.ToString)
TO:
ImgBoxUpdate.InitialImage = My.Resources.ResourceManager.GetObject(Card_File_Name.ToString)
Yea that prevents the image dissapearing part but (at least on my end) it still doesn't work with that modification.
This is rather odd, it should just work.


Has anyone else any idea of whats going on here? Or a suggestion for a simple workaround?
Thanks Nickz for all your help so far. ZipZap will be one of my last open source VB.NET applications for a while (as I am shifting direction to JavaScript, PHP, CSS, and HTML5.) So I really want to make this one count. Full credits will go to you for all/ any assistance provided once it goes live on Sourceforge.
 
Top Bottom