C# 单击后禁用按钮

C# 单击后禁用按钮,c#,winforms,C#,Winforms,我这里有3个按钮 当我点击按钮1时,它将禁用按钮1,然后启用第二个按钮,然后与到达最后一个按钮的过程相同。但我认为我的代码有问题。当我点击第一个按钮时,它不会被禁用 加载时,按钮1启用,按钮2和3禁用 private void groupBox1_Enter(object sender, EventArgs e) { Button btn = sender as Button; if (btn == button1) { button1.Enabled

我这里有3个按钮

当我点击按钮1时,它将禁用按钮1,然后启用第二个按钮,然后与到达最后一个按钮的过程相同。但我认为我的代码有问题。当我点击第一个按钮时,它不会被禁用

加载时,按钮1启用,按钮2和3禁用

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

您订阅的活动是否正确?上面写着groupBox1_进入

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;
        }
    }
}
Button[]buttons=null;//用所有按钮初始化某个地方。
void OnButtonClick(对象发送方,事件参数e)
{
for(int index=0;index
对不起,我错过了你的帖子。按以下顺序

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;
                }
            }
        }
按钮[]按钮=null;
void OnButtonClick(对象发送方,事件参数e)
{
int buttonIndex=Array.IndexOf(按钮,发送方);
for(int index=0;index
检查每个按钮属性中每个按钮的初始值


在更改按钮之前,最好有一个初始值设定项来设置按钮的属性

不要将代码放在
分组框
输入
事件中,此事件将分组显示为
发送方
。订阅其中一个按钮的
ButtonPressed
事件,并最终使用相同的生成方法订阅其他两个按钮的
ButtonPressed
事件(如果您想使用您编写的
if else
语句)

也许您应该这样尝试:

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

这是你认为它必须工作的方式还是我弄错了?

我只是把它放在一个grouppanelbox上,然后使用if-else语句。@user1647667。group box事件将有一个发件人对象groupbox not button。如果你想使用if-else语句,为什么不使用相同的EventHandler方法调用每个按钮呢?你需要使用
点击
事件来触发更改,而不是
分组输入
,也许复选框对一系列操作更直观。我想@user1647667正在尝试像这样做<代码>私有无效按钮3_单击(对象发送者,RoutedEventTargets e){groupBox1_输入(发送者,e);}私有无效groupBox1_输入(对象发送者,RoutedEventTargets e){{//code}}
我不确定!