- [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#] Tự động load user control vào form (Master layout C#)
Xin chào các bạn, bài viết này mình xin hướng dẫn các bạn sử dụng Dynamic load user control vào windows form C#. Hay có thể gọi đơn giản hơn là xây dựng Master layout C#.
Ví dụ: Khi các bạn viết một ứng dụng, mà chỉ có phần nội dung thay đổi, còn header, footer hay sidebar menu đều giống nhau.
Nếu các nào đã từng thiết kế web sẽ hiểu xây dựng master layout như thế nào.
Các bạn có thể xem demo ứng dụng:

Trong ví dụ này, Mình sẽ thiết kế ra một form chính frm_main, và 3 user control: uc_Module1.cs, uc_Module2.cs, uc_Module3.cs
Mỗi usercontrol tương ứng với một module nhé các bạn.
Khi các bạn xây dựng tách biệt ra từng user control, sẽ dễ dàng cho công việc quản lý source code.
Và có thể chia module ra cho từng thành viên code.
Dưới đây là source code form main:
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 LoadUserControlDynamic
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void btnModule1_Click(object sender, EventArgs e)
        {
            if (!panel.Controls.Contains(uc_Module1.Instance))
            {
                panel.Controls.Add(uc_Module1.Instance);
                uc_Module1.Instance.Dock = DockStyle.Fill;
                uc_Module1.Instance.BringToFront();
            }
            else
                uc_Module1.Instance.BringToFront();
        }
        private void btnModule2_Click(object sender, EventArgs e)
        {
            if (!panel.Controls.Contains(uc_Module2.Instance))
            {
                panel.Controls.Add(uc_Module2.Instance);
                uc_Module2.Instance.Dock = DockStyle.Fill;
                uc_Module2.Instance.BringToFront();
            }
            else
                uc_Module2.Instance.BringToFront();
        }
        private void btnModule3_Click(object sender, EventArgs e)
        {
            if (!panel.Controls.Contains(uc_Module3.Instance))
            {
                panel.Controls.Add(uc_Module3.Instance);
                uc_Module3.Instance.Dock = DockStyle.Fill;
                uc_Module3.Instance.BringToFront();
            }
            else
                uc_Module3.Instance.BringToFront();
        }
    }
}Source code cho uc_Module1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LoadUserControlDynamic
{
    public partial class uc_Module1 : UserControl
    {
        private static uc_Module1 _instance;
        public static uc_Module1 Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new uc_Module1();
                return _instance;
            }
        }
        public uc_Module1()
        {
            InitializeComponent();
        }
        private void uc_Module1_Load(object sender, EventArgs e)
        {
        }
    }
}Source code cho uc_Module2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LoadUserControlDynamic
{
    public partial class uc_Module2 : UserControl
    {
        private static uc_Module2 _instance;
        public static uc_Module2 Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new uc_Module2();
                return _instance;
            }
        }
        public uc_Module2()
        {
            InitializeComponent();
        }
        private void uc_Module2_Load(object sender, EventArgs e)
        {
        }
    }
}Source code cho uc_Module3.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LoadUserControlDynamic
{
    public partial class uc_Module3 : UserControl
    {
        private static uc_Module3 _instance;
        public static uc_Module3 Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new uc_Module3();
                return _instance;
            }
        }
        public uc_Module3()
        {
            InitializeComponent();
        }
        private void uc_Module3_Load(object sender, EventArgs e)
        {
        }
    }
}HAVE FUN :)

![[C#] Tự động load user control vào form (Master layout C#)](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Hướng dẫn tạo hotkey cho ứng dụng winform](https://laptrinhvb.net/uploads/source/image_baiviet/85afb64641b1475b65c33e7f190ba0e9.jpg)
![[C#] Thêm ứng dụng vào Taskbar sử dụng SharpShell DeskBand](https://laptrinhvb.net/uploads/source/vbnet/net_monitor_deskband_csharp.gif)
![[C#] Hướng dẫn thêm, xóa, sửa quản lý file Hosts Windows](https://laptrinhvb.net/uploads/source/new_image_baiviet/hosts-file-windows-management.png)
![[C#] Chia sẽ class convert DataTable to List<T> và convet List<T> sang DataTable](https://laptrinhvb.net/uploads/source/csharp/file-conversion.jpg)
![[C#] Hướng dẫn xóa bỏ ký tự đặc biệt ra khỏi chuỗi string trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/special_character_csharp.png)
![[C#] Custom control PictureBox cho phép di chuyển trong Winform](https://laptrinhvb.net/uploads/source/csharp/MovePictureBoxInsideWinForm_thumb%20(1).jpg)
![[C# - DEVEXPRESS] - Cháy túi cùng EURO 2020 - 2021](https://laptrinhvb.net/uploads/source/vbnet/game_euro.jpg)
![[C#] Hướng dẫn tạo hiệu ứng che mờ khuôn mặt (Effect Fixeler Face)](https://laptrinhvb.net/uploads/source/csharp/EffectPixeler_thumb.png)
![[C#] Hướng dẫn download file with progressbar sử dụng windows console](https://laptrinhvb.net/uploads/source/vbnet/download_file_console_csharp.jpg)
![[C#] Chia sẽ tổng hợp source code đồ án về Csharp](https://laptrinhvb.net/uploads/source/vbnet/source_code_doan_csharp.jpg)
![[C#] Chia sẽ thư viện sử dụng Color Picker trong Winform](https://laptrinhvb.net/uploads/source/csharp/color_picker_thumb.png)
![[C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)](https://laptrinhvb.net/uploads/source/new_image_baiviet/hioki_readdata.png)
![[C#] Giới thiệu thư viện Dapper ORM C#](https://laptrinhvb.net/uploads/source/image_baiviet/d766223d56853021bec1c7e958816b2d.jpg)
![[C#] Tự động show Popup các control như: Combobox, DateTimePicker](https://laptrinhvb.net/uploads/source/new_image_baiviet/show_popup.gif)
![[C#] Hướng dẫn sử dụng Row Filter trên DataView của DataTable](https://laptrinhvb.net/uploads/source/csharp/dataview_filter_csharp_thumb.jpg)
![[C#] Chia sẽ Tool Convert Json String to class csharp, vb, typescript](https://laptrinhvb.net/uploads/source/vbnet/json_tool_thumb.png)

![[C#] Hướng dẫn các ẩn thư mục folder trong winform](https://laptrinhvb.net/uploads/source/vbnet/hide_folder_csharp.jpg)
![[C#] Hướng dẫn Split files and Merge files](https://laptrinhvb.net/uploads/source/image_baiviet/24607798908e5ee78a321ade21a83050.png)
![[C#] Mã hóa sử dụng thuật toán triple DES (3DES)](https://laptrinhvb.net/uploads/source/image_baiviet/f0a65cad9fdaf6dfd82399eb11ec96ef.jpg)
![[BÀI 1] Đơn giản hóa lập trình với cơ sở dữ liệu](https://laptrinhvb.net/uploads/source/image_baiviet/c7b293bfd050854c9ff4f5bc865649c4.jpg)

![[C#] Hướng dẫn lấy icon từ thuộc tính file shell32.dll trong windows](https://laptrinhvb.net/uploads/source/image_baiviet/a3747b4af6505b446926aea11fec9d11.jpg)
![[C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/make_trial_soft.png)
![[C#] Hướng dẫn nhúng Embed Windows Explorer vào Winform làm File Manager](https://laptrinhvb.net/uploads/source/csharp/file_manage_thumb.jpg)
