C# 如何通過委託進行視窗傳值
阿新 • • 發佈:2019-02-11
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Form1_Form2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void MyDelegate(object sender, MyEventArgs e);
public event MyDelegate MyEvent;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //例項化一個Form2;
MyEventArgs me=new MyEventArgs(); //自定義的事件資料類例項;
me.MyValue=this.textBox1.Text; //Form1中textBox的值傳給事件資料類例項;
this.MyEvent += new MyDelegate(f2.SetTextValue); //事件訂閱自己建的委託;
MyEvent(this,me); //執行事件;
f2.Show(); //Form2顯示
}
}
public class MyEventArgs:EventArgs
{
public string MyValue="";
}
}