Delegate là một khái niệm khá thú vị và mạnh mẽ trong C. Nó có rất nhiều ứng dụng và truyền dữ liệu giữa các Form là một trong những ứng dụng đó. Nếu bạn đã từng học qua C++ thì bạn sẽ thấy Delegate cũng tương tự như con trỏ hàm trong C++.
Trang 11 Dùng Contructor
Mọi thứ trong C# đều là class, kể cả Form Mà class thì luôn có hàm khởi tạo (Contructor) Ta sẽ lợi dụng điều này để truyền tham số vào Form qua Contructor
Form2 :
Code
public partial class Form2 : Form
{
private string _message;
public Form2()
{
InitializeComponent();
}
public Form2(string Message): this()
{
_message = Message;
lblReceived.Text = _message;
}
}
public partial class Form2 : Form
{
private string _message;
public Form2()
{
InitializeComponent();
}
public Form2(string Message): this()
{
Trang 2_message = Message;
lblReceived.Text = _message;
}
}
Tiếp theo cài đặt trên
Form1 :
Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cmdSend_Click(object sender, EventArgs e) {
Form2 Child = new Form2(txtValue.Text);
Child.Show();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cmdSend_Click(object sender, EventArgs e) {
Form2 Child = new Form2(txtValue.Text);
Child.Show();
Trang 3}
}
Khi nhấn nút Send trên Form1, thông điệp trong textbox sẽ được truyền vào tham số của hàm khởi tạo Form2 Nhờ vậy, thông điệp được truyền vào biến _message của Form2
2 Dùng Properties
Một cách khác để truyền dữ liệu giữa 2 Form là dùng Properties Trong Form2, ta sẽ khai báo một thuộc tính để lưu giữ thông điệp nhận từ Form1 Khi gọi Form2, Form1 sẽ gán thông điệp trực tiếp vào thuộc tính này
Form2 :
Code
public partial class Form2 : Form
{
private string _message;
public Form2()
{
InitializeComponent();
}
public string Message
{
get { return _message; }
set { _message = value; }
}
private void Form2_Load(object sender, EventArgs e)
{
lblReceived.Text = _message;
}
}
public partial class Form2 : Form
{
Trang 4private string _message;
public Form2()
{
InitializeComponent();
}
public string Message
{
get { return _message; }
set { _message = value; }
}
private void Form2_Load(object sender, EventArgs e) {
lblReceived.Text = _message;
}
}
Form1 :
Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cmdSend_Click(object sender, EventArgs e) {
Form2 Child = new Form2();
Child.Message = txtValue.Text;
Child.Show();
}
}
Trang 5public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cmdSend_Click(object sender, EventArgs e)
{
Form2 Child = new Form2();
Child.Message = txtValue.Text;
Child.Show();
}
}
3 Dùng Delegate
Delegate là một khái niệm khá thú vị và mạnh mẽ trong C# Nó có rất nhiều ứng dụng và truyền dữ liệu giữa các Form là một trong những ứng dụng đó Nếu bạn đã từng học qua C++ thì bạn sẽ thấy Delegate cũng tương tự như con trỏ hàm trong C++
Để thực hiện, trong Form2 ta sẽ khai báo một Delegate có nhiệm vụ nhận vào một tham số và không trả về giá trị Đồng thời tạo một hàm để lấy tham số của Delegate Và trong Form1, ta sẽ gọi
Delegate này với tham số truyền vào là một chuỗi thông điệp cần gửi
Form2 :
Code
public partial class Form2 : Form
{
//Khai báo delegate
public delegate void SendMessage(string Message);
public SendMessage Sender; public Form2()
{
InitializeComponent();
Trang 6//Tạo con trỏ tới hàm GetMessage
Sender = new SendMessage(GetMessage); } //Hàm có nhiệm vụ lấy tham số truyền vào private void GetMessage(string Message)
{
lblReceived.Text = Message;
}
}
public partial class Form2 : Form
{
//Khai báo delegate
public delegate void SendMessage(string Message); public SendMessage Sender; public Form2() {
InitializeComponent();
//Tạo con trỏ tới hàm GetMessage
Sender = new SendMessage(GetMessage); } //Hàm có nhiệm vụ lấy tham số truyền vào private void GetMessage(string Message)
{
lblReceived.Text = Message;
}
}
Form1 :
Code
public partial class Form1 : Form
{
public Form1()
{
Trang 7InitializeComponent();
}
private void cmdSend_Click(object sender, EventArgs e) {
Form2 Child = new Form2(); //Tạo Form2
Child.Sender(txtValue.Text); //Gọi delegate
Child.Show();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cmdSend_Click(object sender, EventArgs e) {
Form2 Child = new Form2(); //Tạo Form2
Child.Sender(txtValue.Text); //Gọi delegate
Child.Show();
}
}