SMG SMG -4 years ago 119
C# Question

enable and disable buttons and labels

I have 5 buttons and 5 labels next to each button. When i run the app i expect the first button to be enabled and the the rest disabled and greyed out with the labels. after i click the first button it should disable with the label and enable the second button, and so forth with all the other buttons.

this way is to long, is there a better way of doing this?

private void Form1_Load(object sender, EventArgs e)
{

btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = true;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}

private void btn1_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = true;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = true;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}

private void btn2_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = true;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = true;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
}

private void btn3_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = true;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = true;
lblStep5.Enabled = false;
}

private void btn4_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = true;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = true;
}

private void btn5_Click(object sender, EventArgs e)
{
btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = true;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}

Answer Source

Let all these buttons and labels are inside a container(if it is not means you can use this.Controls as well if the form contains these buttons and labels only). Let it be pnlContainer, Now you can try something like this:

public void ButtonController(Button buttonToEnable, Label labelToenable)
{
    foreach (Control ctrl in panel1.Controls)
    {
        if (ctrl == buttonToEnable || ctrl == labelToenable)
        {
            ctrl.Enabled = true;
        }
        else
        {
            ctrl.Enabled = false;
        }
    }
}

So in Form1_Load you want to enable btn1 and lblStep1 so the call should be :

ButtonController(btn1,lblStep1);

For btn1_Click the method call will be like ButtonController(btn2,lblStep2);. in short you can pass the button and label that you want to enable to this method, which will disable rest of controls in the container.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download