You should make a storage lookup table for the cards. I will give you an example (the names in the example are german, but should be understandable). As a lookup I will use a Dictionary
object that itself holds a collection of dictionaries. That enables the lookup using two different keys. One for the card color and one for the card value. I use Enums to refer to the specific card values to make the code more readable afterwards. When using a variable of type Enum you can either assign an integer value or the name of the entry.
First, the complete code:
Public Class Form1
'Define the enums, example is for a 52 card game (Poker)
Private Enum CardColors
Herz = 1
Karo = 2
Pik = 3
Kreuz = 4
End Enum
Private Enum CardValues
Ass = 1
Zwei = 2
Drei = 3
Vier = 4
Fuenf = 5
Sechs = 6
Sieben = 7
Acht = 8
Neun = 9
Zehn = 10
Bube = 11
Dame = 12
Koenig = 13
End Enum
Private Sub MakeCards()
'Test function for creating a set of test cards, not escpecially relevant to this example
IO.Directory.CreateDirectory("C:\test\cards")
For i = 1 To 4
For y = 1 To 13
Dim bmp As New Bitmap(100, 100)
Using g As Graphics = Graphics.FromImage(bmp)
g.DrawString(CType(i, CardColors).ToString & " " & CType(y, CardValues).ToString, New Font("Arial", 8), Brushes.Black, New Point(10, 10))
End Using
bmp.Save("C:\test\cards\" & CType(i, CardColors).ToString & "_" & y.ToString & ".bmp")
bmp.Dispose()
Next
Next
End Sub
'This dictionary holds the cards
Private Cards As Dictionary(Of CardColors, Dictionary(Of CardValues, Bitmap))
Private Sub InitializeCards()
'The first dictionary contains 4 entries, assigned to the four colors
'The nested dictionaries hold the card values for each color
'We use the ResourceManager.GetObject() function to get the images from the resources
'You of course have to name them with a continuing index for this to work
'In this example: Herz_1, Herz_2, ..., Herz_13, Karo_1, Karo_2, ...
Cards = New Dictionary(Of CardColors, Dictionary(Of CardValues, Bitmap))
For CardColor = 1 To 4
Dim ThisCards As New Dictionary(Of CardValues, Bitmap)
Dim ThisColor As CardColors = CardColor 'This works because the enum is integer based
Dim ThisColorName As String = ThisColor.ToString
For Values = 1 To 13
ThisCards(Values) = My.Resources.ResourceManager.GetObject(ThisColorName & "_" & Values.ToString)
Next
Cards(CardColor) = ThisCards
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Load the cards into the lookup dictionary on startup
InitializeCards()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Show a specific picture in a picturebox
PictureBox1.Image = Cards(CardColors.Herz)(CardValues.Acht)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Select a random card from the range of available cards and show it
Dim rnd As New Random
PictureBox1.Image = Cards(rnd.Next(1, 4))(rnd.Next(1, 13))
End Sub
End Class
So you have to call InitializeCards only once. This loads the cards into the dictionaries. The Initialize function iterates all colors and values and selects the corresponding card from the ressources collection by name and adds it to the respective dictionary.
Afterwards you can very easily select the image of a specific card or a random card.