button not disableing

Jonny123

Member
Joined
Oct 2, 2015
Messages
13
Programming Experience
Beginner
hello,
I am re-creating a application that was posted on youtube. Its a Tic-Tac-Toe game. Here is the link if you are interested:


https://www.youtube.com/watch?v=p3gYVcggQOU


the problem that I am having that happens around 25.28 in the video.


The problem that I am having is that there are 9 buttons in the application to put an "o" or an "X". If there is a winner,
the rest of the buttons are supposed to de-activate by calling a function called disabledButtons. It dose not seem to be working.
AS the chris creates the funtion he realizes that he throws an exception with the function and has to put a try catch into it
because for some reason the menue he has created conflicts with the button.This is the function code:
C#:
        private void disabledButtons()
        {
            try//edit
            {//edit


                foreach (Control c in Controls)
                {
                    Button b = (Button)c;
                    b.Enabled = false;
                }//end for each
            }// end try
            catch { }
        }
it gets called in an if statement




C#:
private void CheckForWinner()
        {
            bool there_is_a_winner = false;


            //horizontal checks
            if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
                there_is_a_winner = true;
            else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
                there_is_a_winner = true;
            else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
                there_is_a_winner = true;


            //vertical checks
            else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
                there_is_a_winner = true;
            else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
                there_is_a_winner = true;
            else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
                there_is_a_winner = true;


            //Diagonal checks
            else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
                there_is_a_winner = true;
            else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled))
                there_is_a_winner = true;
 
if (there_is_a_winner)
            {
                disabledButtons();


                String winner = "";
                if (turn)
                {
                    winner = player2;
                    o_win_count.Text = (Int32.Parse(o_win_count.Text) + 1).ToString();
                }
                else
                {
                    winner = player1;
                    x_win_count.Text = (Int32.Parse(x_win_count.Text) + 1).ToString();
                }
                MessageBox.Show(winner + " wins!", "Yay!");
            }//end if
            else
            {
                if (turn_count == 9)
                {
                    draw_count.Text = (Int32.Parse(draw_count.Text) + 1).ToString();
                    MessageBox.Show("Draw!", "Bummer!");
                }
            }
        }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,792
Location
Sydney, Australia
Programming Experience
10+
Your code assumes that every control on the form is a Button. If an exception is being thrown then obviously that is not the case. Once the exception is thrown, no more controls are processed. If you want to disable the Buttons on the form then don't try to disable controls that aren't buttons. There are a few ways that that could be accomplished, all replacing this:
foreach (Control c in Controls)
{
    Button b = (Button)c;
    b.Enabled = false;
In reverse order of preference, i.e. worst option first:
foreach (Control c in Controls)
{
    if (c is Button)
    {
        c.Enabled = false;
    }
foreach (Control c in Controls)
{
    Button b = c as Button;

    if (b != null)
    {
        b.Enabled = false;
    }
foreach (Button b in Controls.OfType<Button>())
{
    b.Enabled = false;
Note that the first example doesn't need a cast because the Enabled property is a member of the Control class. In the second case, as conditional cast is performed and a Button reference is returned if the object is a Button and null is retruned otherwise. In the last case, the OfType method excludes any items that aren't type Button from the enumeration.
 
Top Bottom