to zhzuo(秋枫) 好,多谢 to turnmissile(会翻跟头的导弹) 用了你的代码测试,delegate没成功。form2中用f1.OnButtonEvent += new OnButtonDelegate(f2.doSomething);这里的f1是新的窗体实例吗? form1 f1; f1.OnButtonEvent += new OnButtonDelegate(f2.doSomething); 报错f1未赋值的变量我又用form1 f1=new form1(); 报错form1,OnButtonEvent(someString);未将对象引用为对象的实例
把form1中的textbox设为public static型,然后在form2中就可以直接调用textbox的值!
当然还有很多别的方法,你可以自己尝试去探索下!
wish u good luck
Greatsft
Form1 中间定义
public delegate void OnButtonDelegate(string str);
publi event OnButtonDelegaet OnButtonEvent;
在按下button之后调用。
OnButtonEvent(someString);Form2构造时加入:
f1.OnButtonEvent += new OnButtonDelegate(f2.doSomething);public void doSomething(string s)
{
showString(s);
}
http://www.csdn.net/Develop/read_article.asp?id=26433
http://www.csdn.net/Develop/read_article.asp?id=26434
http://www.csdn.net/Develop/read_article.asp?id=26436
好,多谢
to turnmissile(会翻跟头的导弹)
用了你的代码测试,delegate没成功。form2中用f1.OnButtonEvent += new OnButtonDelegate(f2.doSomething);这里的f1是新的窗体实例吗?
form1 f1;
f1.OnButtonEvent += new OnButtonDelegate(f2.doSomething);
报错f1未赋值的变量我又用form1 f1=new form1();
报错form1,OnButtonEvent(someString);未将对象引用为对象的实例
form1:
public delegate void OnButtonDelegate(string str);
public event OnButtonDelegate OnButtonEvent;
private void button1_Click(object sender, System.EventArgs e)
{
string str1= textBox1.Text;
OnButtonEvent(str1);
}
form2:
form1 f1;
f1.OnButtonEvent+=new form1.OnButtonDelegate(doSomething);//放在哪里?
public void doSomething(string str)
{
listBox1.Items.Add(str);
}
//用了你的代码测试,delegate没成功。form2中用f1.OnButtonEvent += new
//OnButtonDelegate(f2.doSomething);这里的f1是新的窗体实例吗?
//form1 f1;
//f1.OnButtonEvent += new OnButtonDelegate(f2.doSomething);
//报错f1未赋值的变量//我又用form1 f1=new form1();
//报错form1,OnButtonEvent(someString);未将对象引用为对象的实例
生成form2的时候form1应该已经存在了吧
重写form2的构造函数:
public void form2(Form1 f1)
{
//do something f1.OnButtonEvent += new OnButtonDelegate(this.doSomething);
}这个是我的写法,大家还有更好的吗?
总觉得得到要f1的引用好麻烦
http://www.csdn.net/Develop/read_article.asp?id=26433
http://www.csdn.net/Develop/read_article.asp?id=26434
http://www.csdn.net/Develop/read_article.asp?id=26436