Tải bản đầy đủ (.docx) (2 trang)

Cách truyền dữ liệu giữa 2 form ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (82.33 KB, 2 trang )

CÁCH TRUYỀN DỮ LIỆU GIỮA 2 FORM
Trước hết chúng ta tạo 2 Form

Cách 1: Dùng Constructor
Tại Form 2:
public Form2(string strTextBox)
{
InitializeComponent();
textBox1.Text = strTextBox;
}
Tại Form 1: Click dubble vao button Send Data để viết code cho even Even button1_click
private void button1_Click(object sender, EventArgs e)
{
Form2 fr2 = new Form2(textBox1.Text);
fr2.Show();
}
Cách 2: Dùng delegate
Bước 1: Tại Form 1:
 Tạo deligate: public delegate void TruyenTS(TextBox text);
 Tại Even button1_click:
private void button1_Click(object sender, EventArgs e)
{
Form2 fr2 = new Form2();
TruyenTS truyen = new TruyenTS(fr2.Data);
truyen(textBox1);
fr2.Show();
}
Bước 2: Tại Form 2:
Tạo một hàm delegate đã trỏ tới có nhiệm vụ gán đoạn text cho Textbox1 của Form 2
public void Data(TextBox text)
{


textBox1.Text = text.Text;
}
Kết quả:

×