Thứ Năm, 12 tháng 9, 2019

Mẹo hay dùng

  1. Cách thứ nhất dùng delegate
  • Form1 viết:
public delegate void delPassData(TextBox text);

private void btnSend_Click(object sender, System.EventArgs e)
{
    Form2 frm= new Form2();
    delPassData del=new delPassData(frm.funData);
    del(this.textBox1);
    frm.Show();
}
  • Form2 viết:
public void funData(TextBox txtForm1)
{
    label1.Text = txtForm1.Text;
}
  1. Cách thứ 2 truyền trực tiếp
  • Form1 viết:
private void btnSend_Click(object sender, System.EventArgs e)
{
Form2 frm= new Form2();
//delPassData del=new delPassData(frm.funData); // remove this line
frm.funData(this.textBox1.text()); // passing data directly to function
frm.Show();
}
Mọi người dùng em sự khác nhau giữa 2 cách này ạ
Và khi nào thì nên dùng delegate

Không có nhận xét nào:

Đăng nhận xét