private string s;
public Form5()
{
InitializeComponent();
s = tbx1.Text;
} private void button1_Click(object sender, EventArgs e)
{
//s = tbx1.Text;
MessageBox.Show(s);
}
这个地方,不管tbx1的值如何修改,弹出框的值都不变啊!怎么定义才能让它变呢???当然//s = tbx1.Text;
不能放进click事件里面;因为这个变量s我要在其他界面用
public Form5()
{
InitializeComponent();
s = tbx1.Text;
} private void button1_Click(object sender, EventArgs e)
{
//s = tbx1.Text;
MessageBox.Show(s);
}
这个地方,不管tbx1的值如何修改,弹出框的值都不变啊!怎么定义才能让它变呢???当然//s = tbx1.Text;
不能放进click事件里面;因为这个变量s我要在其他界面用
//这是Form5的构造函数,在构造函数中,你为变量s赋了初值.
public Form5()
{
InitializeComponent();
s = tbx1.Text;
}
//在button1_Click的事件中,你没为给变量s赋值,MessageBox.Show(s)显示出来的值当然都不会变了.
private void button1_Click(object sender, EventArgs e)
{
//s = tbx1.Text;
MessageBox.Show(s);
}
你说的:“怎么定义才能让它变呢”,你的目的是想让s的值变吗?
只要你给s赋值,它就会变;你不给s赋值,他是不会变的。我猜你的目的是想用s来实时放tbx1.Text的值。如果是这样,你可以在tbx1的MouseLeave事件中实时的给s=tbx1.Text赋值。那么在button1_Click事件中,MessageBox.show(s)就会有变化.
{
s = tbx1.Text;
}
对于4楼:textchange本窗体FORM5中可以即时显示更新;
但是发现有问题,因为在另外一个界面的函数中,用到了Form5 fm5 = new Form5();然后调用这个fm5.S;还是不变;
//主窗体内容
private string s;
public Form5()
{
InitializeComponent();
s = tbx1.Text;
}
public string S
{
get { return s; }
}
//另外一个类内的使用;
Form5 fm5 = new Form5();
Method(fm5.S);我是希望修改了tbx1.Text的值后, Method(fm5.S)中的参数fm5.S可以修改;
可能思路错了;
怎么办啊???
放到MessageBox.Show(s);
的上面,或者敢不敢把MessageBox.Show(s);改成MessageBox.Show(tbx1.Text);
private string s;
public Form5()
{
InitializeComponent();
s = tbx1.Text;
} private void button1_Click(object sender, EventArgs e)
{
//s = tbx1.Text;
MessageBox.Show(tbx1.Text); }
public Form5()
{
InitializeComponent();
s = tbx1.Text;
} private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(tbx1.Text);//这里直接使用tbx的值就行了
}
private string s;
public Form5()
{
InitializeComponent();
s = tbx1.Text;
}
public string S
{
get { return tbx1.Text; }
}