public class Form1 : System.Windows.Forms.Form//两个窗体之1 { static string OPC;//静态变量用来保存 public string xx { set{OPC=value;} get{return OPC;} } } public class Form2 : System.Windows.Forms.Form//两个窗体之2 { Form1 form1=new Form1(); form1.xx=要传递的值; } 这个时候当执行了Form2后,Form2的值就保存在Form1中的OPC里。
在Form1中生明如下方法:
private void button1_Click(object sender, System.EventArgs e)
{
Form f2=new Form2();
f2.Show ();
this.textBox1.Text =f2.label1.Text;
}在编译的时侯,结果为错误显示为 "F:\projects\forExample\Form1.cs(237): 不可访问“forExample.sub.label1”,因为它受保护级别限制"
就是说 this.textBox1.Text =f2.label1.Text;这一句
"可以在主form中加一个事件,每创建一个子form, 就把自己的处理函数加入主form 的事件委托中. 很方便的, 是设计模式中的 "发布者/观察者"模式." 这是什么意思啊,大哥?
我太菜了,不太明白!想知道,哪位大哥,能够赐教!
gz
比如:
Form f2=new Form2(int i);
f2.Show ();
当然在form2的构造函数你也要写
form2(int i)
{}
你也可以用性质:
比如:
在form2中写
private int i;
public geti
{
get{return(i))
set(i= value)
}
关于你的错误,是因为label2在f2的定义中是private,你把他改成public就行了
this.textBox1.Text =f2.label1.Text;
这是什么意思啊,有人能详细解释一下吗?
class MainForm
{
public event EventHandler NotifySubForm;
}在 子Form的构造函数中,把自己加入主Form 的事件处理中.class Form2
{
Form2(MainForm mainForm)
{
mainForm.NotifySubForm += new EventHandler(this.DoSomething);
}
private void DoSomething(...)
{
}
}
这样,只要在主Form 中调用 NotifySubForm(..) 就会自动调用所有子Form的处理函数.
{
static string OPC;//静态变量用来保存
public string xx
{
set{OPC=value;}
get{return OPC;}
}
}
public class Form2 : System.Windows.Forms.Form//两个窗体之2
{
Form1 form1=new Form1();
form1.xx=要传递的值;
}
这个时候当执行了Form2后,Form2的值就保存在Form1中的OPC里。
怎么定义NotifySubForm啊.555555..................还是不会,
{
static string OPC;//静态变量用来保存
public string xx
{
set{OPC=value;}
get{return OPC;}
}
}
public class Form2 : System.Windows.Forms.Form//两个窗体之2
{
Form1 form1=new Form1();
form1.xx=要传递的值;
}
这个时候当执行了Form2后,Form2的值就保存在Form1中的OPC里。
问,可是我需要这个数据的如果一个LABEL控件呢,怎么让form2的一个动作来实现,form1中label的数据更新呢.
谁还有好的建议,说出来大伙听听/