- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[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 :)