cs项目中窗体间方法的传递
1 public partial class Form1 : Form 2 { 3 //声明委托,用于窗体间方法的传递 4 public delegate void DelegateLoadview(); 5 6 public Form1() 7 { 8 InitializeComponent(); 9 } 10 //窗体加载时 11 private void Form1_Load(object sender, EventArgs e) 12 { 13 LoadGridview(); 14 } 15 16 //绑定数据 17 private void LoadGridview() 18 { 19 List<ASRS_User> list = bll.Sel(); 20 this.dataGridView1.DataSource = list; 21 } 22 23 24 //添加产品 25 private void btnAdd_Click(object sender, EventArgs e) 26 { 27 DelegateLoadview dld = new DelegateLoadview(LoadGridview); 28 FAdd fd = new FAdd(dld); 29 fd.ShowDialog(); 30 }
}
窗体2中的代码
public partial class FAdd : Form { private Form1.DelegateLoadview dld; public FAdd() { InitializeComponent(); } public FAdd(Form1.DelegateLoadview dld) : this() { // TODO: Complete member initialization this.dld = dld; } //确定 private void button1_Click(object sender, EventArgs e) { string num = txtNum.Text.Trim(); if (num == "") { MessageBox.Show("请输入"); } else { //刷新数据 dld(); } } }
完成form1窗体中方法的调用了