C#中两个窗体之间怎么传递数据?

来源:百度知道 编辑:UC知道 时间:2024/06/22 02:36:12
我写一个程序是这样的:
主界面是Form1,点击Form1上的一个小一点的pictureBox,弹出一个窗口(Form2),在Form2上有一个大的pictureBox,里面显示Form1的pictureBox里的图片,只是放大了。
我是这样写的,
在Form1里
Form2 f=new Form2();
if (System.IO.File.Exists(s)) //s是我的图片的路径
{
f.pictureBox.load(s);
f.Show();
}

在Form2里:
public PictureBox pictureBox
{
get
{
return this.pictureBox1;
}
}

我的问题是,我怎么在Form2中得到我在Form1中的那个s图片的尺寸?
在Form1中我是这样写的:
Image pic=Image.FormFiles(s);
int intWidth=pic.Width;
int intHeight=pic.Height;
我怎样才能将这里的intWidth和intHeight传递到Form2中去???
"
改Form2的构造函数。把intWidth和intHeight通过构造函数传进去。

回答者: duanwy - 魔法师 四级
"
能帮写一下代码吗?谢谢!!

在Form2里:
public void showpic(string s ,int intWidth,int intHeight)
{
this.pictureBox1.load(s);

...
}

//form1
f.showpic(s,intWidth,intWidth);

可以使用类的静态变量,或者事件通知机制
还可以使用类的构造函数进行参数传递

改Form2的构造函数。把intWidth和intHeight通过构造函数传进去。

Form2.cs:
private int _height;
private int _width
public Form2(int height,int width)
{
_height = height;
_width = width;
}
//Form2里就可以用_height和_width了

Form1.cs:
Image pic=Image.FormFiles(s);
int intWidth=pic.Width;
int intHeight=pic.Height;
//
Form2 f=new Form2(intHeight,intWidth);
//后面不变

一楼正解

form2 中
internal form1 aaaForm;

form1中
Form2 f=new Form2();
if (System.IO.File.Exists(s)) //s是我的图片的路径
{
f.pictureBox.load(s);
f.aaaForm =this;
f.Show();
}

调用 aaaForm.pictureBox.Width 就是form1窗体上pictureBox的宽度。