- [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 đệ quy Recursion trong lập trinh Winform
Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn sử dụng đệ quy bằng cách sử dụng Callback Function trong lập trình C# Winform.
[C#] Recursion Demo Winform
Vậy đệ quy là gì? tại sao chúng ta cần phải sử dụng đệ quy.
Đệ quy (Recursion) là một trong những giải thuật khá quen thuộc trong lập trình, mở rộng ra là trong toán học (thường được gọi với tên khác là “quy nạp”).
Có một số bài toán, buộc phải sử dụng đệ quy mới giải quyết được, chẳng hạn như duyệt cây.
Thông thường, trong lập trình Web, khi xây dựng cây Menu động, chúng ta bắt buộc phải sử dụng đệ quy để thực hiện công việc một cách hiệu quả.
Bây giờ, mình có một ví dụ sau đây.
Dưới đây, chúng ta sẽ có một cây menu bằng SubItemBar trong Devexpress như hình bên dưới:
 
 
Và yêu cầu của chúng ta là làm sao đọc cây menu thành một bảng table gồm 2 field: id và parent 
Bằng cách bình thường, chúng ta sẽ sử dụng vòng lặp For để quét từng trường của dữ liệu.
Tuy nhiên, mỗi menu có chiều sâu khác nhau, nên ở trường hợp này chúng ta không thể nào sử dụng for để truy vấn hết được dữ liệu ở cây menu trên.
=> dùng để cho phân quyền ứng dụng của mình.
và kết quả khi chúng ta thực hiện đệ quy xong, chúng ta sẽ được một cây thư mục treeview như hình bên dưới.

Dưới đây là hình ảnh sử dụng đệ quy: cứ thấy barSubItem là nó tiếp tục gọi lại hàm chính nó EnumAllSubitems.
Ở tham số thứ 2: chúng ta sẽ thấy một hàm callback, được truyền vào ở parametter thứ hai.
Action<BarItem, BarSubItem> callback
Dưới đây là full source code đệ quy C#, demo ứng dụng trên:
using DevExpress.Skins;
using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DeQuy_Demo
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        public DataTable CreateTable()
        {
            var table = new DataTable();
            table.Columns.Add("id", typeof(string));
            table.Columns.Add("parent_id", typeof(string));
            table.Columns.Add("caption", typeof(string));
            return table;
        }
        public void EnumAllSubitems(BarSubItem subItem, Action<BarItem, BarSubItem> callback)
        {
            foreach (BarItemLink link in subItem.ItemLinks)
            {
                BarItem item = link.Item;
                callback(item, subItem);
                if (item is BarSubItem)
                {
                    EnumAllSubitems((BarSubItem)item, callback);
                }
            }
        }
        private void btn_load_Click(object sender, EventArgs e)
        {
            var table = CreateTable();
            EnumAllSubitems(menu_system, (subItem, parent) =>
            {
                if (subItem != null)
                {
                    var parent_id = parent.Name;
                    var item_caption = subItem.Caption;
                    var item_name = subItem.Name;
                    table.Rows.Add(item_name, parent_id, item_caption);
                }            
             });
            treeList1.DataSource = table;
            treeList1.ViewStyle = DevExpress.XtraTreeList.TreeListViewStyle.TreeView;         
            treeList1.KeyFieldName = "id";
            treeList1.ParentFieldName = "parent_id";
            treeList1.ExpandAll();
            treeList1.OptionsView.ShowTreeLines = DevExpress.Utils.DefaultBoolean.True;
        }
    }
}
Thanks for watching!

![[C#] Hướng dẫn sử dụng đệ quy Recursion trong lập trinh Winform](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.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#] Hướng dẫn sử dụng phím tắt trong Visual Studio With SublimeVS extension](https://laptrinhvb.net/uploads/source/csharp/keyword_sublime_thumb.jpg)
![[C#] Hướng dẫn tạo Windows Services đơn giản Winform](https://laptrinhvb.net/uploads/source/vbnet/windows_services_thumb.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#] Hướng dẫn Split files and Merge files](https://laptrinhvb.net/uploads/source/image_baiviet/24607798908e5ee78a321ade21a83050.png)
![[C#] Tạo hiệu ứng hình ảnh Image Cartoon Effect](https://laptrinhvb.net/uploads/source/csharp/effect_image_cartoon_thumb.png)
![[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#] Hướng dẫn lấy icon từ process](https://laptrinhvb.net/uploads/source/image_baiviet/ac285a18a0dc4d0b19db80c67d292a04.png)
![[C#] Chia sẽ Class System Info lấy thông tin phần cứng của máy tính](https://laptrinhvb.net/uploads/source/csharp/hard_ware_thumb.png)
![[C#] Hướng dẫn đăng nhập website sử dụng Http Request with Authenticate Username and password](https://laptrinhvb.net/uploads/source/image_baiviet/f7629c89018cb90f8c9dc8c512dc3a2d.png)
![[C#] Lập trình thay đổi ngày giờ hệ thống Windows](https://laptrinhvb.net/uploads/source/csharp/date_time_setting_thumb.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#] Hướng dẫn lập trình upload file  webserver php sử dụng WebClient UploadFileAsync](https://laptrinhvb.net/uploads/source/csharp/upload_file_thumb.jpg)
![[C#] Hướng dẫn kiểm tra tường lửa  (FireWall) trong lập trình Csharp](https://laptrinhvb.net/uploads/source/image_baiviet/284a34f31a0e2374b3d72759a21c8ee9.jpg)
![[C#] Hướng dẫn sử dụng Color Dialog để lấy mã màu lập trình Winform](https://laptrinhvb.net/uploads/source/csharp/color_diaglog_thumb.png)

![[C#] Hướng dẫn chèn video Youtube vào App Winform](https://laptrinhvb.net/uploads/source/csharp/youtube_embble_thumb.jpg)
![[C#] Hướng dẫn tạo tab ứng dụng giống Chrome sử dụng thư viện EasyTabs](https://laptrinhvb.net/uploads/source/csharp/tab_google_thumb.png)
![[C#] Hướng dẫn giới hạn số cửa sổ ứng dụng khi chạy trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/limit_instance_app_thumb.png)
![[C#] Hướng dẫn code hiển thị thời gian thực trên Winform](https://laptrinhvb.net/uploads/source/vbnet/time_relative.png)
![[TUTS] Tắt giao diện thập niên 90s của trang web StackOverFlow nghi vấn bị Hack](https://laptrinhvb.net/uploads/source/csharp/stack_over_flow_thumb.jpg)
![[C#] Liệt kê danh sách font chữ được cài đặt vào máy tính](https://laptrinhvb.net/uploads/source/image_baiviet/c7bd5b3a8caeed378ecdd9f558c0c617.jpg)
![[C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/thumb_auto_fill.png)
![[C#] Hướng dẫn  sử dụng Regular Expression](https://laptrinhvb.net/uploads/source/image_baiviet/36e94c9623b615fa7218a4816e13c714.png)
