1. Trang chủ
  2. » Công Nghệ Thông Tin

truyền dữ liệu trong các form c

7 443 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

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++.

1 Dùng Contructor Mọi thứ C# class, kể Form Mà class có hàm khởi tạo (Contructor) Ta lợi dụng điều để 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() { _message = Message; lblReceived.Text = _message; } } Tiếp theo cài đặt 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(); } } Khi nhấn nút Send Form1, thông điệp textbox truyền vào tham số hàm khởi tạo Form2 Nhờ vậy, thông điệp truyền vào biến _message Form2 Dùng Properties Một cách khác để truyền liệu Form dùng Properties Trong Form2, ta khai báo thuộc tính để lưu giữ thông điệp nhận từ Form1 Khi gọi Form2, Form1 gán thông điệp trực tiếp vào thuộc tính 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 { 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; } } 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(); } } 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(); } } Dùng Delegate Delegate khái niệm thú vị mạnh mẽ C# Nó có nhiều ứng dụng truyền liệu Form ứng dụng Nếu bạn học qua C++ bạn thấy Delegate tương tự trỏ hàm C++ Để thực hiện, Form2 ta khai báo Delegate có nhiệm vụ nhận vào tham số không trả giá trị Đồng thời tạo hàm để lấy tham số Delegate Và Form1, ta gọi Delegate với tham số truyền vào 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(); //Tạo 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 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() { 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(); } } 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(); } }

Ngày đăng: 22/08/2016, 16:42

Xem thêm: truyền dữ liệu trong các form c

TỪ KHÓA LIÊN QUAN

w