Hi to all


I would like to ask for help with my C# project. It is a Memory game involving pictures. First the user clicks a picture box and the image shows and remains displayed, then the user clicks another picture box to find the first's pair. I'm having problem with checking if the pair selected is wrong. the first selected picture doesn't "turn back" to the default state.:confused:

i somehow discovered that if i display a messagebox in the mouseup event before checking the pictures, the incorrect pair "turns back" to the default state.

how can i do this without using MessageBox?? i tried system.threading.thread.sleep() but it doesn't work


here is the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        int clicked = 0;
        string[] name = new string[4];
        System.Collections.ArrayList picbox = new System.Collections.ArrayList();
        string[] picpath = new string[4] { "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg", "C:\\Users\\Public\\Pictures\\Sample Pictures\\Hydrangeas.jpg", "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg", "C:\\Users\\Public\\Pictures\\Sample Pictures\\Hydrangeas.jpg" };
        string name1 = "", name2 = "";
        int score = 0;



        public Form1()
        {
            InitializeComponent();
            hide();
        }

        public void ThirdClick()
        {
            if (name1 != name2)
            {
                foreach (string x in picbox)
                {
                    switch (x)
                    {
                        case "picbox1": pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.Chrysanthemum;
                            break;
                        case "picbox2": pictureBox2.Image = WindowsFormsApplication1.Properties.Resources.Chrysanthemum;
                            break;
                        case "picbox3": pictureBox3.Image = WindowsFormsApplication1.Properties.Resources.Chrysanthemum;
                            break;
                        case "picbox4": pictureBox4.Image = WindowsFormsApplication1.Properties.Resources.Chrysanthemum;
                            break;

                    }
                    
                    
                    
                }
                
            }
            else if (name1 == name2)
            {
                foreach (string y in picbox)
                {
                    switch (y)
                    {
                        case "picbox1": pictureBox1.Enabled = false;
                            pictureBox1.BackColor = Color.AliceBlue;
                            break;
                        case "picbox2": pictureBox2.Enabled = false;
                            pictureBox2.BackColor = Color.AliceBlue;
                            break;
                        case "picbox3": pictureBox3.Enabled = false;
                            pictureBox3.BackColor = Color.AliceBlue;
                            break;
                        case "picbox4": pictureBox4.Enabled = false;
                            pictureBox4.BackColor = Color.AliceBlue;
                            break;

                    }


                }
                score++;
                MessageBox.Show(score.ToString());


            }

            picbox.RemoveRange(0, 2);
            name1 = ""; name2 = "";



        }

        public void hide()
        {
            pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.Chrysanthemum;
            pictureBox2.Image = WindowsFormsApplication1.Properties.Resources.Chrysanthemum;
            pictureBox3.Image = WindowsFormsApplication1.Properties.Resources.Chrysanthemum;
            pictureBox4.Image = WindowsFormsApplication1.Properties.Resources.Chrysanthemum;



        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (clicked == 0)
            {
                pictureBox1.ImageLocation = picpath[0];
                picbox.Add("picbox1");
                clicked = 1;
                name1 = picpath[0];


            }
            else if (clicked == 1)
            {
                pictureBox1.ImageLocation = picpath[0];
                picbox.Add("picbox1");
                clicked = 2;
                name2 = picpath[0];

            }
            
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (clicked == 0)
            {
                pictureBox2.ImageLocation = picpath[1];
                picbox.Add("picbox2");
                clicked = 1;
                name1 = picpath[1];


            }
            else if (clicked == 1)
            {
                pictureBox2.ImageLocation = picpath[1];
                picbox.Add("picbox2");
                clicked = 2;
                name2 = picpath[1];

            }
            
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (clicked == 0)
            {
                pictureBox3.ImageLocation = picpath[2];
                picbox.Add("picbox3");
                clicked = 1;
                name1 = picpath[2];


            }
            else if (clicked == 1)
            {
                pictureBox3.ImageLocation = picpath[2];
                picbox.Add("picbox3");
                clicked = 2;
                name2 = picpath[2];

            }
            
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            if (clicked == 0)
            {
                pictureBox4.ImageLocation = picpath[3];
                picbox.Add("picbox4");
                clicked = 1;
                name1 = picpath[3];


            }
            else if (clicked == 1)
            {
                pictureBox4.ImageLocation = picpath[3];
                picbox.Add("picbox4");
                clicked = 2;
                name2 = picpath[3];

            }
            
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (clicked == 2)
            {
                foreach (string x in picbox)
                {
                    MessageBox.Show(x);

                }
                
                ThirdClick();
                clicked = 0;

                if (score == 2)
                {
                    MessageBox.Show("FINISHED");
                    Application.Exit();
                }
                


            }
        }

        private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            if (clicked == 2)
            {
                foreach (string x in picbox)
                {
                    MessageBox.Show(x);

                }
                
                ThirdClick();
                clicked = 0;

                if (score == 2)
                {
                    MessageBox.Show("FINISHED");
                    Application.Exit();
                }
                


            }
        }

        private void pictureBox3_MouseUp(object sender, MouseEventArgs e)
        {
            if (clicked == 2)
            {
                foreach (string x in picbox)
                {
                    MessageBox.Show(x);

                }
               
                ThirdClick();
                clicked = 0;

                if (score == 2)
                {
                    MessageBox.Show("FINISHED");
                    Application.Exit();
                }
                


            }
        }

        private void pictureBox4_MouseUp(object sender, MouseEventArgs e)
        {
            if (clicked == 2)
            {
                foreach (string x in picbox)
                {
                    MessageBox.Show(x);

                }
                

                ThirdClick();
                clicked = 0;

                if (score == 2)
                {
                    MessageBox.Show("FINISHED");
                    Application.Exit();
                }
                

            }
        }

    }
}

Edited 5 Years Ago by gorosh: n/a

first of all i need to say there is a lot of code here which is not neccessary and need optrmization so try to edesign your code
but for your problem try this.Invalidate()

This article has been dead for over six months. Start a new discussion instead.