请教编程高手,关于窗体间传值的问题
下面是我写的代码,请各位帮忙看看哪里有错误,千恩万谢啊!
-------------------------------------
传送类,代码:
using System;
namespace lib
{
public class Class1
{
private static string data;
public Class1()
{
}
public static string Data
{
get{return data;}
set{data=value;}
}
}
}
A窗体按钮传送,代码:
private void button1_Click(object sender, System.EventArgs e)
{
Class1.Data =textBoxA.Text ;
}
B窗口接收,代码:
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
textBox1.Text =Class1.Data;
}
---------------------------
不知道为什么,就是传送不了,请高手解答
------解决方案--------------------不好意思, 没注意
你这种传值需要用委托:
class{
public delegate TextChangedHandler(string data); //声明委托类型
public static TextChangedHandler OnTextChanged; //声明委托类型变量
public static string Data
{
get{return data;}
set{data=value;
if (OnTextChanged != null)
OnTextChanged(data);
}
}
//A窗体按钮传送,代码:
//Form2 B = new Form2();
//B.Show();
private void button1_Click(object sender, System.EventArgs e)
{
Class1.OnTextChanged += new TextChangedHandler(B.TextChanged); //注册委托
Class1.Data =textBoxA.Text ;
}
//B窗体
public void TextChanged(string data)
{
this.TextBox1.Text = data;
}
}