c#-启用和禁用按钮,基于某些条件的标签

我在每个按钮旁边有5个按钮和5个标签.当我运行该应用程序时,我希望第一个按钮被启用,其余的按钮被禁用,并带有灰色标签.在我单击第一个按钮后,应禁用带有标签的标签并启用第二个按钮,以此类推.

这种方法很长,有更好的方法吗?

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

解决方法:

让所有这些按钮和标签都放在容器内(如果不是,则意味着您可以使用this.Controls,如果表单仅包含这些按钮和标签).使其成为pnlContainer,现在您可以尝试执行以下操作:

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

因此,在Form1_Load中,您要启用btn1和lblStep1,因此调用应为:

ButtonController(btn1,lblStep1);

对于btn1_Click,方法调用将类似于ButtonController(btn2,lblStep2);.简而言之,您可以将要启用的按钮和标签传递给此方法,这将禁用容器中的其余控件.

上一篇:linux-使用cmd重命名带有数字的txt文件作为文件名


下一篇:C#从CSV文件读取字符串并绘制折线图