[英]How to make button invisible?
使用工具箱創建的Button02。 Button03在Method111內以編程方式創建,我可以使用Button03的visible屬性,但是當我在另一個方法中(比如說Method222())時,我不能使用visible屬性。 這是出於上下文。 我正在使用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
,例如,作為類成員。
我不知道為什么可以訪問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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.