- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[C#] Hướng dẫn sử dụng delegate để truyền dữ liệu giữa 2 form
Bài viết hôm nay, mình xin hướng dẫn các bạn sử dụng Delegate trong C# để truyền dữ liệu qua lại giữa hai form với nhau. Ngoài ra các bạn có thể dùng từ khòa Static để truyền dữ liệu giữa các class.
C# HƯỚNG DẪN TRUYỀN DỮ LIỆU GIỮA 2 FORM
Nhưng trong bài viết này, mình sử dụng Delegate C#
Giao diện demo ứng dụng:

Source code Form 1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DelegateDemo
{
    public delegate void SendMessage(String value);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // Ta tạo một delegate để tham chiếu
            // Tiếp theo ta tạo một phương thức dùng để set giá trị cho textbox form chính
            // Ta tạo thêm một form mới
            Form2 f2 = new Form2(SetValue);
            // Ta truyền hàm SetValue qua form 2, và form 2 khi nhấn nút button bên đó sẽ gọi lại phương thức SetValue bên này
            f2.ShowDialog();
        }
        private void SetValue(String value)
        {
            this.textBox1.Text = value;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}Source code cho form 2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DelegateDemo
{
    public partial class Form2 : Form
    {
        // Bên form 2 ta tạo thêm một delegate dùng để truyền hàm delegate bên form chính qua
        public SendMessage send;
        public Form2()
        {
            InitializeComponent();
        }
        // Tạo một phương thức khởi tạo mới
        public Form2(SendMessage sender)
        {
            // Phương thức khởi tạo này ta sẽ gọi ở bên form 1
            InitializeComponent();
            this.send = sender;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // Tại sự kiện nhấn nút button bên form 2, ta sẽ gọi lại hàm delegate
            this.send(this.textBox1.Text);
            // ta gọi như trên để gọi làm hàm bên form 1, và truyền giá trí this.txtBox1.Text qua form 1
        }
        private void Form2_Load(object sender, EventArgs e)
        {
        }
    }
}Các bạn có thể thiết kế 2 form như giao diện bên trên vào copy 2 đoạn code này vào 2 form rồi chạy ứng dụng.
Chúc các bạn thành công!

![[C#] Hướng dẫn sử dụng delegate để truyền dữ liệu giữa 2 form](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Hiệu ứng gõ phím Smooth typing Effect RichTextBox giống MS Word 2013](https://laptrinhvb.net/uploads/source/csharp/smooth_typing_csharp_thumb.png)
![[C#] Tạo SecureString TextBox trong winform](https://laptrinhvb.net/uploads/source/csharp/secure_string_thumb.png)
![[C#] Source code phần mềm Food Order sử dụng FlowLayoutPanel và Usercontrol](https://laptrinhvb.net/uploads/source/vbnet/dat_mon_thumb.jpg)
![[C#] Hướng dẫn gọi hàm bằng chuỗi String sử dụng Method.Invoke](https://laptrinhvb.net/uploads/source/csharp/add_form_to_panel_thumb.png)
![[C#] Hướng dẫn bắt sự kiện khi đối tượng di chuyển chạm vào object trên winform HitTest Object](https://laptrinhvb.net/uploads/source/new_image_baiviet/hitTest_object.gif)
![[C#] Hướng dẫn lập lịch tự động Task Schedule trong Windows](https://laptrinhvb.net/uploads/source/csharp/Task_Schedule_thumb.png)
![[C#] Hướng dẫn sử dụng Sprite Image hiển thị hình Gif Sticker Zalo](https://laptrinhvb.net/uploads/source/csharp/zalo_sprite_csharp.gif)
![[C#] Hướng dẫn chèn text vào dưới hình ảnh winform](https://laptrinhvb.net/uploads/source/csharp/watermask_image_csharp_thumb.png)
![[C#] Hướng dẫn thêm nút button vào thanh taskbar ứng dụng khi Minimize](https://laptrinhvb.net/uploads/source/image_baiviet/5803a62ba6bf1932351a886f913e8446.jpg)
![[C#] Hướng dẫn thay đổi giao diện ứng dụng Winform theo giao diện của Windows](https://laptrinhvb.net/uploads/source/new_image_baiviet/CHANGE_THEME_THUMB.png)
![[C#] Hướng dẫn lập trình gởi tin nhắn zalo sử dụng api](https://laptrinhvb.net/uploads/source/csharp/zalo_messager_thumb.jpg)
![[C#] Chia sẻ source code tool kiểm tra domain website](https://laptrinhvb.net/uploads/source/new_image_baiviet/PING_DOMAIN_THUMB.png)
![[C#] Empty Recycle Bin trong lập trình csharp](https://laptrinhvb.net/uploads/source/image_baiviet/5ffdaade6ca87e295ce10f4bcb715c04.jpg)
![[C#] Hướng dẫn tạo hiệu ứng rung chuyển form (Attention or buzz effect winform)](https://laptrinhvb.net/uploads/source/csharp/attention_effect_csharp_thumb.jpg)
![[C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/momo_danang.png)
![[C#] Hướng dẫn viết tools tự động upload video lên Youtube  - using Api v3 Csharp](https://laptrinhvb.net/uploads/source/image_baiviet/166b02ef5dab9e7402d9068374ca40d0.png)
![[C#] Hướng dẫn sử dụng Api v3 google để đăng bài tự động lên blogger, blogspot](https://laptrinhvb.net/uploads/source/image_baiviet/c4873ac823455e9f76265c74b38a36cd.png)

![[C#] Chạy số 0 - 9 có hiệu ứng nhanh chậm Random Number Winform](https://laptrinhvb.net/uploads/source/csharp/random_number_effect.gif)
![[C#] Hướng dẫn sử dụng thư  viện Microsoft WindowsAPICodePack](https://laptrinhvb.net/uploads/source/image_baiviet/5d7b706fe239b97ac0157cd0bedfd9a6.jpg)

![[C#] Tải thư viện Dll từ Web server vào ứng dụng Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/dynamic_dll_csharp.png)
![[C#] Hướng dẫn load hình ảnh vào picture box chế độ Unlock](https://laptrinhvb.net/uploads/source/image_baiviet/3ddeac9c43a69536767cc2870f28b4f1.png)
![[C#] Hướng dẫn chuyển đổi chuỗi sang nhị phân và ngược lại](https://laptrinhvb.net/uploads/source/image_baiviet/20c1faac8453ae1604adc8c2daf7d391.jpg)
![[C#] Hướng dẫn tạo checkbox Datagridview và truyền dữ liệu giữa hai Gridview](https://laptrinhvb.net/uploads/source/image_baiviet/82698fb5a7cd0b476d1c0f2dee94c9be.jpg)
