- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
- [DATABASE] Tách số và chữ từ chuỗi - hàm tối ưu hóa tách số và chữ trong Sqlserver
- [C#] Tìm kiếm gần đúng Full Text Search sử dụng thư viện Lucene.NET
- [C#] Chia sẻ tài liệu, sdk và source code máy chấm công dòng máy ZKTeco
- [C#] Memory Cache là gì, và sử dụng trong ứng dụng Winform
- [DATABASE] Khóa chính Primary Key trong Sqlserver
[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!