C# Console app - How do I make an interactive menu?(C# 控制台应用程序 - 如何制作交互式菜单?)
问题描述
我正在尝试制作一个类似于以下内容的菜单:
I'm trying to make a menu that looks similar to:
> Thing
Another Thing
Yet Another Thing
Exit
在 C# 控制台应用程序中.我已经查看了所有内容,但找不到此类菜单的库/示例代码.
Within a C# console app. I've looked all over but can't find a library/example code for this sort of menu.
简单来说,我想...
- 显示菜单
- 允许用户使用箭头键 [UP/DOWN] 进行选择
- 选择不同的选项时做不同的事情[Enter]
推荐答案
主要是能够捕获输入.控制台只能通过清除控制台窗口并重新渲染来模拟"交互式菜单.
Main thing is to be able to capture inputs. Console can only "simulate" an interactive menu by clearing the console window and re-rendering it again.
using System.Collections.Generic;
using System;
using System.Linq;
using System.Threading;
namespace ConsoleApp
{
class Program
{
public static List<Option> options;
static void Main(string[] args)
{
// Create options that you want your menu to have
options = new List<Option>
{
new Option("Thing", () => WriteTemporaryMessage("Hi")),
new Option("Another Thing", () => WriteTemporaryMessage("How Are You")),
new Option("Yet Another Thing", () => WriteTemporaryMessage("Today")),
new Option("Exit", () => Environment.Exit(0)),
};
// Set the default index of the selected item to be the first
int index = 0;
// Write the menu out
WriteMenu(options, options[index]);
// Store key info in here
ConsoleKeyInfo keyinfo;
do
{
keyinfo = Console.ReadKey();
// Handle each key input (down arrow will write the menu again with a different selected item)
if (keyinfo.Key == ConsoleKey.DownArrow)
{
if (index + 1 < options.Count)
{
index++;
WriteMenu(options, options[index]);
}
}
if (keyinfo.Key == ConsoleKey.UpArrow)
{
if (index - 1 >= 0)
{
index--;
WriteMenu(options, options[index]);
}
}
// Handle different action for the option
if (keyinfo.Key == ConsoleKey.Enter)
{
options[index].Selected.Invoke();
index = 0;
}
}
while (keyinfo.Key != ConsoleKey.X);
Console.ReadKey();
}
// Default action of all the options. You can create more methods
static void WriteTemporaryMessage(string message)
{
Console.Clear();
Console.WriteLine(message);
Thread.Sleep(3000);
WriteMenu(options, options.First());
}
static void WriteMenu(List<Option> options, Option selectedOption)
{
Console.Clear();
foreach (Option option in options)
{
if (option == selectedOption)
{
Console.Write("> ");
}
else
{
Console.Write(" ");
}
Console.WriteLine(option.Name);
}
}
}
public class Option
{
public string Name { get; }
public Action Selected { get; }
public Option(string name, Action selected)
{
Name = name;
Selected = selected;
}
}
}
这篇关于C# 控制台应用程序 - 如何制作交互式菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 控制台应用程序 - 如何制作交互式菜单?
基础教程推荐
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 创建属性设置器委托 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01