日期:2014-05-17 浏览次数:21249 次
//Form2中委托代码 public delegate void GetDatahandle(List<string>abc); //声明委托 public partial class importdata : Form { public event GetDatahandle setString; //定义一个与委托相关的事件 private void button2_Click(object sender, EventArgs e) { List<string> rv = new List<string>(); string de="aaa,fff" rv.Add(de); wage cx1 = new wage(); //setString += new GetDatahandle(cx1.SetViewValue); //订阅一个事件 if (setString != null)// 触发事件 setString(rv); this.Close(); } } //在Form1的委托处理事件中 [color=#FF6600]private void btnShowForm2(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.setString += new GetDatahandle(this.SetViewValue); f2.ShowDialog(); }[/color] public void SetViewValue(List<string> cx) { dataGridView1.RowCount = cx.Count + 1; char[] es = new char[] { ',' }; for (int j = 0; j < cx.Count; j++) { string[] ak = cx[j].Split(es); if (ak[0].Equals("0")) dataGridView1.Rows[j].Cells[0].Value = " "; else { dataGridView1.Rows[j].Cells[0].Value = ak[0]; } if (ak[1].Equals("0")) dataGridView1.Rows[j].Cells[1].Value = " "; else dataGridView1.Rows[j].Cells[1].Value = ak[1]; } Application.DoEvent(); }
------解决方案--------------------
真乱啊。。。怎么在Form2里自己绑定委托实例啊。不应该是Form1里绑定Form2的事件?
wage cx1 = new wage();
setString += new GetDatahandle(cx1.SetViewValue); //订阅一个事件
wage 是什么?