c#窗体间传值

可通过如下3种方法,供你参考。
1. 通过设立公共变量访问
从 Form1 传到 Form3


public class Common
{
public static Image MyImage;
}
//Form1
public partial class Form1 : Form
{


private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3(); 
Common.MyImage = this.pictureBox1.Image;
frm3.Show();
}
}
//Form3
public partial class Form3 : Form
{
private void Form3_Load(object sender, EventArgs e)
{


if (Common.MyImage != null)
{
this.pictureBox1.Image = Common.MyImage;
}
}
}


2.通过窗体类的属性传递
从 Form1 传到 Form3
//Form1
public partial class Form1 : Form
{


private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.MyImage = this.pictureBox1.Image;
frm3.Show();
}
}
//Form3
public partial class Form3 : Form
{
public Image MyImage { get; set; }


private void Form3_Load(object sender, EventArgs e)
{
if (MyImage != null)
{
this.pictureBox1.Image = MyImage;


}


} }
3.通过委托与事件触发传递
从 Form3 通过事件触发传到 Form1
public delegate void PassInfoDelegate(Image myImage);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();


frm3.FireInfoEvent += frm3_FireInfoEvent;


frm3.Show();
}


void frm3_FireInfoEvent(Image myImage)
{
if (this.IsHandleCreated)
{
this.BeginInvoke(new PassInfoDelegate(ShowInfo), new object[] { myImage });
}
}


private void ShowInfo(Image myImage)
{
this.pictureBox1.Image = myImage;
}
}


public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public event PassInfoDelegate FireInfoEvent;


private void button1_Click(object sender, EventArgs e)
{


PassInfo(this.pictureBox1.Image );


}
private void PassInfo(Image myImage)
{
if (FireInfoEvent != null)
{
FireInfoEvent(myImage);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值