Hi...! I'm Working on mind challenge game i nearly cmepleted it but i've got a problem...
when a player choose a button it shows it's number but when it choose the second button it won't show the second button number because of this lines:

 foreach (var ctrl in this.Controls)
                    {
                        if ((ctrl as Button).Text == Convert.ToString(compareclass.getfirstnumber()) || (ctrl as Button).Text == Convert.ToString(compareclass.getsecondnumber()))
                        {
                            (ctrl as Button).BackColor = Color.Wheat;
                            (ctrl as Button).ForeColor = Color.Wheat;
                        }
                    }
                    this.Text = "Wrong!";

so i want a little dealy after the second button is clicked that player can see both buttons number so how can i do tha?!
this 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;
using System.Collections;
using System.Threading;

namespace MindChallange
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class Btn
        {
            private int[] btn;
            public Btn()
            {
                btn = new int[36];
            }
            public void setnumbers(int n,int num)
            {
               btn[n]=num; 
            }
            public int getnumber(int n)
            {
                return btn[n];
            }

        }
        public class Compare
        {
            int first, second;
            public Compare()
            {
            }
            public void firstnumber(int f)
            {
                first = f;
            }
            public void secondnumber(int s)
            {
                second = s;
            }
            public int getsecondnumber()
            {
                return second;
            }
            public int getfirstnumber()
            {
                return first;
            }
            public bool cmp()
            {
                if (first == second) return true;
                return false;
            }
        }
        public class Position
        {
            private int X, Y;
            public Position(int x, int y)
            {
                X = x;
                Y = y;
            }
            public Position() { }
            public int getX()
            {
                return X;
            }
            public int getY()
            {
                return Y;
            }

        }
        Random rnd = new Random();
        ArrayList arrlist = new ArrayList();
        ArrayList arrlista = new ArrayList();
        Btn Buttonclass = new Btn();
        public static class test
        {

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            int[] rndnum = new int[36];
            rndnum[0] = rnd.Next(1, 18);
            arrlist.Add(rndnum[0]);
            for (int i = 1; i <= 17; i++)
            {
                rndnum[i] = rnd.Next(1, 18);

                    while (arrlist.Contains(rndnum[i]))
                    {
                        rndnum[i] = rnd.Next(1, 19);
                    }
                arrlist.Add(rndnum[i]);            
            }
            rndnum[18] = rnd.Next(1, 18);
            arrlista.Add(rndnum[18]);
            for (int j = 19; j <= 35; j++)
            {
                rndnum[j] = rnd.Next(1, 18);
                while (arrlista.Contains(rndnum[j]))
                {
                    rndnum[j] = rnd.Next(1, 19);
                }
                arrlista.Add(rndnum[j]); 
            }
            Button[] btn = new Button[36];

            int x = 80;
            int y = 80;
            int line = 0;
            for (int k = 0; k <= 35; k++)
            {
                btn[k]=new Button();
                btn[k].Width = 50;
                btn[k].Height = 50;
                btn[k].Location = new Point(x, y);
                btn[k].Text = Convert.ToString(rndnum[k]);
                btn[k].BackColor = Color.Wheat;
                btn[k].ForeColor = Color.Wheat;
                Controls.Add(btn[k]);
                Buttonclass.setnumbers(k,rndnum[k]);
                Position temp = new Position();
                btn[k].Tag = temp;
                this.Text = this.Text + btn[k].Tag;
                btn[k].Click+=new EventHandler(Button_click);
                x += 90;
                line++;
                if (line % 6 == 0)
                {
                    y += 67;
                    x = 80;
                }
            }

        }
        int change = 0;
        Compare compareclass = new Compare();
        int choosecounter = 0;
        private void Button_click(object sender, EventArgs e)
        {
            Position pos = new Position();
            pos = (Position)((Button)sender).Tag;
            choosecounter++;
            change++;
            if (change % 2 == 0)
            {
                compareclass.secondnumber(Convert.ToInt32((sender as Button).Text));
                (sender as Button).ForeColor = Color.Red;
                if (compareclass.cmp())
                {
                    this.Text = "Same";
                    foreach (var ctrl in this.Controls)
                    {
                        if ((ctrl as Button).Text == Convert.ToString(compareclass.getfirstnumber()) || (ctrl as Button).Text == Convert.ToString(compareclass.getsecondnumber()))
                        {
                            (ctrl as Button).Enabled = false;
                        }
                    }

                }
                else
                {
                    foreach (var ctrl in this.Controls)
                    {
                        if ((ctrl as Button).Text == Convert.ToString(compareclass.getfirstnumber()) || (ctrl as Button).Text == Convert.ToString(compareclass.getsecondnumber()))
                        {
                            (ctrl as Button).BackColor = Color.Wheat;
                            (ctrl as Button).ForeColor = Color.Wheat;
                        }
                    }
                    this.Text = "Wrong!";

                }
            }
            else
            {
                compareclass.firstnumber(Convert.ToInt32((sender as Button).Text));
                (sender as Button).ForeColor = Color.Red;
            }
            int o = 0;
            foreach (var butt in this.Controls)
            {
                if (!(butt as Button).Enabled)
                {
                    o++;
                    if (o == 36)
                    {
                        DialogResult d = MessageBox.Show("You Won In " + choosecounter + " Clicks! \n Would You Like To Restart?", "You Won!", MessageBoxButtons.YesNo);
                        if (d == DialogResult.Yes)
                        {
                            Application.Restart();
                        }
                        else
                        {
                            Application.Exit();
                        }
                    }
                }

            }
        }
    }
}

Thanks...!

There are a couple of options, the simplest is probably Thread.Sleep. You might find it easier to write and read your code if you cast the control to the type you want once as a new control, then use the properties of that control, rather than casting it each time you want to access the properties.

Here's some revised code to look at:

    private void Button_click(object sender, EventArgs e)
    {
        Button ClickedButton = (Button)sender;
        Position pos = new Position();
        pos = (Position)(ClickedButton.Tag;
        choosecounter++;
        change = !change;
        if (change == false)
        {
            compareclass.secondnumber(Convert.ToInt32(ClickedButton.Text));
            ClickedButton.ForeColor = Color.Red;
            ClickedButton.Refresh();
            Thread.Sleep(2000);
            if (compareclass.cmp())
            {
                this.Text = "Same";
                foreach (Button ctrl in this.Controls.OfType<Button>())
                {
                    if (ctrl.Text == Convert.ToString(compareclass.getfirstnumber()) || ctrl.Text == Convert.ToString(compareclass.getsecondnumber()))
                    {
                        ctrl.Enabled = false;
                    }
                }
            }
            else
            {
                foreach (Button ctrl in this.Controls.OfType<Button>())
                {
                    if (ctrl.Text == Convert.ToString(compareclass.getfirstnumber()) || ctrl.Text == Convert.ToString(compareclass.getsecondnumber()))
                    {
                        ctrl.BackColor = Color.Wheat;
                        ctrl.ForeColor = Color.Wheat;
                    }
                }
                this.Text = "Wrong!";
            }
        }
        else
        {
            compareclass.firstnumber(Convert.ToInt32(ClickedButton.Text));
            ClickedButton.ForeColor = Color.Red;
        }
        int o = 0;
        foreach (Button butt in this.Controls.OfType<Button>())
        {
            if (!butt.Enabled)
            {
                o++;
                if (o == 36)
                {
                    DialogResult d = MessageBox.Show("You Won In " + choosecounter + " Clicks! \n Would You Like To Restart?", "You Won!", MessageBoxButtons.YesNo);
                    if (d == DialogResult.Yes)
                    {
                        Application.Restart();
                    }
                    else
                    {
                        Application.Exit();
                    }
                }
            }
        }
    }

Edited 3 Years Ago by tinstaafl

This question has already been answered. Start a new discussion instead.