-2

I'm attempting to make a blackjack type clone in c# so I need to make my cards appear randomly as the game loads up.

This is what I have tried, to no avail. Cardgen is simply a random number generator, textbox1 is simply something that displays a "game over" and the picturebox1 is the card.

        if (cardGen == 2)
        {
            pictureBox1.Image = Properties.Resources._2;

            score += 2;

            if (score > 21)
            {
                textBox1.Visible = true;
            }

        } 
C Dog
  • 29
  • 3
  • 3
    and what was the result? no image appear in the picture box? do you have an image with this name in the resources? are you getting any errors? – Amr Elgarhy Dec 04 '16 at 22:15
  • Hey, sorry. No image appeared, yes I do have an image with that name and no, I'm not getting any compiler errors. – C Dog Dec 04 '16 at 22:50
  • It is hard to determine what is the issue here, but it worth double checking all trivial points, such as making sure that cardGen=2 and debug through your code line by line, you may find the solution easily or give us a better description of the issue. – Amr Elgarhy Dec 04 '16 at 22:58
  • Load image from Disk and confirm that works first. Then narrow down why the Image Resource isn't loading. I suggest you [load an image resource at Design-Time](http://stackoverflow.com/a/29176427/495455) and then goto InitializeComponent and see the code the designer creates and copy that. – Jeremy Thompson Dec 04 '16 at 23:09
  • Thank you to both of you for the answers and I've half answered my own question now. I've realised I put it under "picturebox_click" so if I click on the image it appears. Sorry I didn't post more code, it's my first time using forms and I thought that part would be irrelevant. Having said that now I'm not quite sure how to fix this problem. – C Dog Dec 04 '16 at 23:23

1 Answers1

0

First, I added 4 images to my resources. Then, added a random number generator. With the generated number, different pictures are shown in the picturebox.

    private void Form1_Load(object sender, EventArgs e)
    {
        Random rnd = new Random();
        int randomNumber = rnd.Next(1, 4);
        switch (randomNumber)
        {
            case 1:
                pictureBox1.Image = Properties.Resources._01;
                break;

            case 2:
                pictureBox1.Image = Properties.Resources._02;
                break;

            case 3:
                pictureBox1.Image = Properties.Resources._03;
                break;

            case 4:
                pictureBox1.Image = Properties.Resources._04;
                break;
        }
    }
active92
  • 644
  • 12
  • 24