Reputation: 151
I am making a card game where I have created 10 different cards, I have implemented the pictures into the program in the resources area on the right. My card structure looks like this:
public Form1()
{
cards = new PlayCard[10];
cards[0] = new PlayCard("Harry", 97, 33, 40);
cards[1] = new PlayCard("Dave", 80, 91, 41);
cards[2] = new PlayCard("Randle", 90, 13, 45);
cards[3] = new PlayCard("Zach", 30, 98, 81);
cards[4] = new PlayCard("Shorty", 30, 89, 99);
cards[5] = new PlayCard("Matt", 75, 81, 72);
cards[6] = new PlayCard("Aaron", 89, 82, 80);
cards[7] = new PlayCard("Ryan", 25, 83, 71);
cards[8] = new PlayCard("Stephen", 96, 100, 100);
cards[9] = new PlayCard("David", 90, 37, 70);
InitializeComponent();
}
I'm wondering how I get the corresponding picture to show up determined on which card is shown
Thanks
Upvotes: 0
Views: 2762
Reputation: 23831
Use an ImageList
. You can then name each image to correspond with your names. There is a good introduction to ImageLists and how to add images at run-time and design-time here.
First add images (shown at run-time):
imageList1.Images.Add("pic1", Image.FromFile("c:\\mypic.jpg"));
To remove image from collection:
imageList1.Images.RemoveAt(listBox1.SelectedIndex);
imageList1.Images..RemoveByKey("pic1");
To access images, get image from the image collection:
panel1.BackgroundImage = imageList1.Images["David"];
or for your array:
panel1.BackgroundImage = imageList1.Images[cards[i][0]];
I hope this helps.
Edit. to address the comments of the above not being OOP. Instead of using an ImageList
you could add an Image
to your PlayCard
object. There is also an option to use a Dictionary<string, Image>
to handle this mapping, but again this could be construed as non-OOP.
Upvotes: 2
Reputation: 782
Add the image as a property to your PlayCard class, then assign an image to each playcard (easiest way - change your constructor). Each playcard will know its image, so it's really easy to implement it in your existing code without many modifications.
Upvotes: 0