C#在两个窗体中变量引用的问题

afgghepss9 2006-05-30 03:33:31
在C#开发windows应用程序窗体名为form1;form2;我怎么样才能互相引用两个窗体的公共变量,form1是先打开的,form2是后面打开的 我想在form2引用form1的变量请问大侠门有什么办法,给个例子是最好的,本人菜鸟
...全文
167 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
marvelstack 2006-05-30
  • 打赏
  • 举报
回复
可以参考这里的文章,
在.net中轻松掌握Windows窗体间的数据交互
http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx
xiaoni_309 2006-05-30
  • 打赏
  • 举报
回复
http://www.51one.net/study/cc/2705.html

看这个,有详细介绍
fellowcheng 2006-05-30
  • 打赏
  • 举报
回复
Form1内:
//要访问的变量,假如是a
public string a;
Form2 f2;

private void 生成Form2()
{
f2 = new Form2(this);

}

Form2内:

Form1 f1;
public Form2(Form1 f)
{
this.f1 = f;
}

private void reada()
{
MessageBox.Show(this.f1.a);
}

fellowcheng 2006-05-30
  • 打赏
  • 举报
回复
普通公共变量的话,只有把Form做为参数了
Form1:
//要访问的变量,假如是a
public string a;
Form2 f2;

private void 生成Form2()
{
f2 = new Form2(this);

}

Form1:

Form1 f1;
public Form2(Form1 f)
{
this.f1 = f;
}

private void reada()
{
MessageBox.Show(this.f1.a);
}
fangxiangting 2006-05-30
  • 打赏
  • 举报
回复
在form1中定义一个form2的实例。这样在form1中就可以引用form2中的变量了
class Form1
{
Form2 form2;
}
bhwhy 2006-05-30
  • 打赏
  • 举报
回复
变量设为public
在form2中引用form1
form1 frm=new from1()
frm.变量名
fellowcheng 2006-05-30
  • 打赏
  • 举报
回复
static 的是最方便的,Form1.变量名 的方式

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧