1
Reply

How do I compare user input to an image in Image[ ]

momo akhouri

momo akhouri

Aug 30 2012 8:33 AM
1.2k
Hey!

I'm having a bit of trouble in making a "guess the flag" game in C#.

I have managed to create a picturebox in which random pictures (national flags) are displayed when a button is clicked.

However, I need to be able to make it so that the user can guess which country the flag belongs to, e.g. if the american flag appears, then the user can input "america" in a textbox and the progam should determine whether it is the correct answer or not.

Essentially, my question is:

How do I compare textbox.text with the image that appears by the Random() class?

My code:

public partial class Form1 : Form
    {


        Random r = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            //Array of images
            Image[] images = new Image[3];
            images[0] = Image.FromFile(@"C:\Users\Momo\Desktop\flags 1\india.png");
            images[1] = Image.FromFile(@"C:\Users\Momo\Desktop\flags 1\australia.png");
            images[2] = Image.FromFile(@"C:\Users\Momo\Desktop\flags 1\america.png");

           
            int random = r.Next(0, 3);

            //set picturebox to random
            pictureBox1.Image = images[random];



         }
       

Thank you

Answers (1)
Next Recommended Forum