◆访问变量问题,请高手帮我修改如下程序◆
//窗体上有textBox1和button1控件,
//执行button1_Click事件后为何textBox1的text值还是为空?如何修改MyClass?
using System;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form //
{
public Form1()
{
InitializeComponent();
}
public class MyClass
{
public void MyFunc()//
{
Form1 app = new Form1();
app.textBox1.Text = "aa ";
}
}
private void button1_Click(object sender, EventArgs e)
{
MyClass obj = new MyClass();
obj.MyFunc();//执行完毕后为何textBox1的text值还是空?
}
}
}
------解决方案--------------------两个form对象当然不行,改成
public class MyClass
{
public void MyFunc(Form1 app)//
{
app.textBox1.Text = "aa ";
}
}
MyClass obj = new MyClass();
obj.MyFunc(this);
------解决方案--------------------直接这样不更省事:
//窗体上有textBox1和button1控件,
//执行button1_Click事件后为何textBox1的text值还是为空?如何修改MyClass?
using System;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form //
{
public Form1()
{
InitializeComponent();
}
public void MyFunc()//
{
textBox1.Text = "aa ";
}
private void button1_Click(object sender, EventArgs e)
{
MyFunc();
}
}
}
------解决方案--------------------这样的话,还可以简单
//窗体上有textBox1和button1控件,
//执行button1_Click事件后为何textBox1的text值还是为空?如何修改MyClass?
using System;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form //
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)