ボタン(Button1)がオンのフォーム(form1)があります。C#:参照の使用
例1では、Button1のテキストプロパティがform1の「CHANGED」に変更されますが、例2は変更されないのはなぜですか?なぜ違いがありますか?
例1:
namespace WindowsFormsApplication35
{
public partial class Form1 : Form
{
Button b1 = new Button();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
b1 = button1;
b1.Text = "CHANGED";
}
}
}
例2:他のながら
namespace WindowsFormsApplication35
{
public partial class Form1 : Form
{
Button b1 = new Button();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1 = b1;
b1.Text = "CHANGED";
}
}
}
追加this.Controls.Add(b1);コードの副作用を実際に見ることができるようにコンストラクタに渡します。 –
あなたはそれを詳しく教えてもらえますか? – Michael