user1647667
user1647667

Reputation: 1309

disabling button after clicking it

i have here 3 buttons.

when i click on the button 1, it will disable button 1 then enable the second button, then same proces as it reach the last button. but i think there is something wrong with my code. it doesn't disable when i click on the first button

button1 is enabled and button2 and 3 is disabled when it loads.

private void groupBox1_Enter(object sender, EventArgs e)
{
    Button btn = sender as Button;

    if (btn == button1)
    {
        button1.Enabled = false;
        button2.Enabled = true;
        button3.Visible = false;
        button3.Enabled = false;
        MessageBox.Show("button 1 disabled");
    }
    else if (btn == button2)
    {
        button1.Enabled = false;
        button2.Enabled = false;
        button3.Visible = true;
        button3.Visible = true;
        MessageBox.Show("button 2 disabled");
    }
    else if (btn == button3)
    {
        button1.Enabled = false;
        button2.Enabled = false;
        button3.Visible = false;
        button3.Visible = false;
        MessageBox.Show("button 3 disabled");
    }
}

Upvotes: 1

Views: 3331

Answers (4)

xalcatraazx
xalcatraazx

Reputation: 3

Maybe you should try it this way:

private void groupBox1_Enter(object sender, EventArgs e)
{
    Button btn = sender as Button;

    if (btn == button1)
    {
        button1.Enabled = false;
        button2.Enabled = true;
        button3.Enabled = false;
        MessageBox.Show("button 1 is disabled");
    }
    else if (btn == button2)
    {
        button1.Enabled = false;
        button2.Enabled = false;
        button3.Enabled = true;
        MessageBox.Show("button 1 & button 2 are disabled");
    }
    else if (btn == button3)
    {
        button1.Enabled = false;
        button2.Enabled = false;
        button3.Enabled = false;
        MessageBox.Show("button 3 disabled");
    }
}

is this the way you think it has to work or am i getting this wrong?

Upvotes: -1

Tobia Zambon
Tobia Zambon

Reputation: 7629

Don't put the code inside the Enter event of the GroupBox, this event will have the groupoBow as sender. Subscribe the ButtonPressed event of one of the button and eventually use the same generated method to subscribe the ButtonPressed event of the 2 other buttons (if you want to use the if-else statements as you wrote)

Upvotes: 0

Mullaly
Mullaly

Reputation: 320

Check what are the initial values of each button in the properties of each button.

Its better to have a Initializer to set the properties of the button before changing it.

Upvotes: 0

AbdElRaheim
AbdElRaheim

Reputation: 1394

Are you subscribing to the right event? It says groupBox1_Enter.

Button[] buttons = null;  // Initialize somewhere with all the buttons.

void OnButtonClick(object sender, EventArgs e)
{
    for (int index = 0; index < buttons.Length; index++)
    {
        if (buttons[index] == sender)
        {
            buttons[index].Enabled = buttons[index].Visible = false;
        }
        else
        {
            buttons[index].Enabled = buttons[index].Visible = true;
        }
    }
}

Sorry I miss read your post. In order below

Button[] buttons = null;
        void OnButtonClick(object sender, EventArgs e)
        {
            int buttonIndex = Array.IndexOf(buttons, sender);
            for (int index = 0; index < buttons.Length; index++)
            {
                if (index == buttonIndex + 1)
                {
                    buttons[index].Enabled = buttons[index].Visible = true;
                }
                else
                {
                    buttons[index].Enabled = buttons[index].Visible = false;
                }
            }
        }

Upvotes: 4

Related Questions