使用按钮控制命令的顺序

时间:2014-06-03 09:47:48

标签: c# .net winforms window command

在Windows格式中,我有Button1Button2Button3。这些按钮表示应该按照从开始操作到最终操作的顺序执行的一系列操作。所以我通常可以按照以下方式处理。

Form1: Form
{

    Form_Load(Object sender, event Args e)
    {
        Button1.Enabled = true;
        Button2.Enabled = false;
        Button3.Enabled = false;
    }


    Button1_click(Object sender, event Args e)
    {
        //Actions
        Button2.Enabled = true;
        Button1.Enabled = false;
    }

    Button2_click(Object sender, event Args e)
    {
        //Actions
        Button3.Enabled = true;
        Button2.Enabled = false;
    }

    Button3_click(Object sender, e)
    {
        //Actions
        Button3.Enabled = false;
        Button1.Enabled = true;
    }
}

在某些地方,我是这样做的。这是标准方式吗?

修改

此外,在您点击button1之前应该点击button2之类的简单情况,上述方法是否可以接受?

1 个答案:

答案 0 :(得分:1)

为了扩展Bjarke所说的内容,我提供了一个代码示例。

Form1: Form
{
    List<Button> listButtons = new List<Button>();

    public void EnableButton(Button btnToEnable)
    {
        foreach(Button btn in listButtons)
        {
            //check button name.
            //if it is the button to enable, enable it, if not then disable it
            btn.Enabled = btn.Name == btnToEnable.Name;
        }
    }

    Form_Load(Object sender, event Args e)
    {
        listButtons.Add(Button1);
        listButtons.Add(Button2);
        listButtons.Add(Button3);

        EnableButton(Button1);

        //Button1.Enabled = true;
        //Button2.Enabled = false;
        //Button3.Enabled = false;
    }


    Button1_click(Object sender, event Args e)
    {
        EnableButton(Button2);
        //Actions
        //Button2.Enabled = true;
        //Button1.Enabled = false;
    }

    Button2_click(Object sender, event Args e)
    {
        EnableButton(Button3);
        //Actions
        //Button3.Enabled = true;
        //Button2.Enabled = false;
    }

    Button3_click(Object sender, e)
    {
        EnableButton(Button1);
        //Actions
        //Button3.Enabled = false;
        //Button1.Enabled = true;
    }
}