如何让按钮不可见?

How to make button invisible?

Button02 使用工具箱创建。 Button03 以编程方式创建 在 Method111 中,我可以使用 Button03 的可见 属性,但是当我在另一个方法中(比方说 Method222())时,我不能使用可见 属性。说的是断章取义。我正在使用 C#

private void Method111()
{
    Button Button03 = new Button();
    Button03.Size = Button02.Size;
    Button03.Location = new Point(Button02.Location.X + Button02.Width + A02,
                                  Button02.Location.Y);
    Button03.Visible = true;
    Button03.Text = "";
    Controls.Add(Button03);
    Button03.Click += (sender, args) =>
    {
    };
}

Button03 变量是您当前函数作用域的局部变量。这意味着您无法访问此函数之外的变量。

为了解决这个问题,您需要在可以从两个函数访问的某些范围内声明 Button03,例如作为 class 会员。

我不知道你为什么可以访问Button02,因为你没有发布包含声明的代码。但是,我的假设是您的代码看起来像这样:

public class SomeClass
{
    public Button Button02;

    private void Method111()
    {
        Button Button03;
        // Button03 is accessible because it is declared in this method
        // Button02 is accessible because it is declared in this class
    }

    private void SomeOtherMethd()
    {
        // Button03 is not accessible because it was declared in other method's scope
        // Button02 is accessible because it is declared in this class
    }
}