- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
[C#] Hướng dẫn tạo menu trên Console App
Xin chào các bạn, bài viết hôm nay mình tiếp tục chia sẻ các bạn cách tạo menu trên Console C#.
[C#] How to create menu in Console
Dưới đây là giao diện demo ứng dụng Tạo menu trên Console C#:
Ở trong bài viết này, mình sử dụng thư viện ConsoleMenu các bạn có thể cài đặt từ Nuget.
PM> NuGet\Install-Package ConsoleMenu -Version 0.1.1
Trong bài viết, mình có tạo hai menu, khi các bạn chọn danh mục thì nó sẽ vào Submenu
Source code menu console C#:
using ConsoleTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace Menu_Console
{
internal class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Console.ForegroundColor = ConsoleColor.Green;
var subMenu = new ConsoleMenu(args, level: 1)
.Add("Tôn giáo", () => SomeAction("Sub_One"))
.Add("Tình thành", () => SomeAction("Sub_Two"))
.Add("Quận Huyện", () => SomeAction("Sub_Three"))
.Add("Học vấn", () => SomeAction("Sub_Four"))
.Add("Đóng", ConsoleMenu.Close)
.Configure(config =>
{
config.Selector = "--> ";
config.EnableFilter = true;
config.Title = "Submenu";
config.EnableBreadcrumb = true;
config.WriteBreadcrumbAction = titles => Console.WriteLine(string.Join(" / ", titles));
});
var menu = new ConsoleMenu(args, level: 0)
.Add("Danh sách nhân viên", () => SomeAction("Danh sách nhân viên"))
.Add("Bảng lương", () => SomeAction("Bảng lương"))
.Add("Lịch công tác", () => SomeAction("Three"))
.Add("Danh mục", subMenu.Show)
.Add("Change me", (thisMenu) => thisMenu.CurrentItem.Name = "I am changed!")
.Add("Đóng", ConsoleMenu.Close)
.Add("Thực hiện hành động rồi đóng", (thisMenu) => { SomeAction("Close"); thisMenu.CloseMenu(); })
.Add("Thoát ứng dụng", () => Environment.Exit(0))
.Configure(config =>
{
config.Selector = "--> ";
config.EnableFilter = true;
config.Title = "============== DANH SÁCH MENU - LAPTRINHVB.NET ===============";
config.EnableWriteTitle = false;
config.EnableBreadcrumb = true;
});
menu.Show();
}
static void SomeAction(string text) {
Console.WriteLine(text);
Console.ReadKey();
}
}
}
Danh sách các tham số cấu hình cho menu:
public class MenuConfig
{
public ConsoleColor SelectedItemBackgroundColor = Console.ForegroundColor;
public ConsoleColor SelectedItemForegroundColor = Console.BackgroundColor;
public ConsoleColor ItemBackgroundColor = Console.BackgroundColor;
public ConsoleColor ItemForegroundColor = Console.ForegroundColor;
public Action WriteHeaderAction = () => Console.WriteLine("Pick an option:");
public Action<MenuItem> WriteItemAction = item => Console.Write("[{0}] {1}", item.Index, item.Name);
public string Selector = ">> ";
public string FilterPrompt = "Filter: ";
public bool ClearConsole = true;
public bool EnableFilter = false;
public string ArgsPreselectedItemsKey = "--menu-select=";
public char ArgsPreselectedItemsValueSeparator = '.';
public bool EnableWriteTitle = false;
public string Title = "My menu";
public Action<string> WriteTitleAction = title => Console.WriteLine(title);
public bool EnableBreadcrumb = false;
public Action<IReadOnlyList<string>> WriteBreadcrumbAction = titles => Console.WriteLine(string.Join(" > ", titles));
}
Thanks for watching!