Hi...!
As in the title i want to know that how i can check that all the buttons in my form are disable?!

Recommended Answers

All 3 Replies

Walk through the Controls collection of your form,
test if a control is a button
test if all are disabled

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;
            }
        }
        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]);

                Controls.Add(btn[k]);
                Buttonclass.setnumbers(k,rndnum[k]);
                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)
        {

            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).ForeColor = Color.Black;
                        }
                    }
                    this.Text = "Wrong!";

                }
            }
            else
            {
                compareclass.firstnumber(Convert.ToInt32((sender as Button).Text));
                (sender as Button).ForeColor = Color.Red;
            }
            foreach (var butt in this.Controls)
            {
                if (!(butt as Button).Enabled)
                {
                    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();
                    }
                }

            }
        }
    }
}

look at this...
it say"You Won" When two button is disable...
what is wrong with it?

if (!(butt as Button).Enabled) <-- This results as "TRUE" on the first button that is disabled so it will display your message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.