Sign in to follow this  
KingRage

Help VB loading images

Recommended Posts

KingRage    100
So I'm making a game of blackjack. How do I make it so that say card1= the king of hearts so that the imgCard1 loads the image King of Hearts. Also how would I store all 52 images of the cards? Thanks. P.S. if you can post how to create random numbers too that would be good thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Another newbie, just like me (at least to VB.NET)! I'm writing a simple VB backgammon game, so I'm running into similar issues.

Remember for all your questions, try to look them up first in the VB Help->Index.

Re: Random numbers

Paraphrased from the Visual Basic Language Reference:
----------------------------------------
'Use the Rnd function to generate a random integer value from 1 to 52.

Dim intCardNumber1 As Integer
intCardNumber1 = CInt(Int((52 * Rnd()) + 1))
----------------------------------------

Also, check "Randomize" in the Help Index -- you need to call it once (say in your Form Load event subroutine) to initialize the random numbers.

Re: Images

Someone else might have to answer this, but maybe you can be more specific about what you are trying to do. Are you building a Windows Form application and wanting to put images into a Picture Box control?

If so, you can just use the Image.FromFile(strfilename) method. What I did is wrote a local subroutine to clean it up a bit. Here it is:
-----------------------------
Sub LoadPictureBoxImage(ByVal pictbox As PictureBox, ByVal strfilename As String)
' Do a smart load of a picture box from an image file.
' The name of the image file is stored in the Tag property of the PictureBox, and
' the image is only reloaded if the Tag property differs from the target file name.
If CStr(pictbox.Tag) strfilename Then
pictbox.Image = Image.FromFile(strfilename)
pictbox.Tag = strfilename
End If
End Sub
-----------------------------
Then I just call it to load whatever image I need when I need it, as for example something like:

LoadPictureBoxImage(PictureBox_Card1, strGamePicturesPathDflt & strPictFileName_KingOfHearts)

I haven't gotten as far as storing all the images in an array of image objects (for my game, the images are pretty static) but I'm sure it's possible. (I checked out ImageList class, but it seems to be oriented to animation. However, you might be able to use it with a little creativity.)

Good luck.

--PaulG


Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
In my above reply, the symbol in "CStr(pictbox.Tag) strfilename" was stripped out.

(Maybe it doesn't like less-than and greater-than symbols, which could be a problem when trading code.)

--PaulG

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Huh, it did it again. It stripped out the not-equal-to symbol.

Moderator, is there a way to ensure that LESS-THAN and GREATER-THAN symbols are not stripped out of replies? Thx.

--PaulG

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this