简体   繁体   English

C# 控制台应用程序 - 如何制作交互式菜单?

[英]C# Console app - How do I make an interactive menu?

I'm trying to make a menu that looks similar to:我正在尝试制作一个类似于以下内容的菜单:

> Thing
  Another Thing
  Yet Another Thing
  Exit

Within a C# console app.在 C# 控制台应用程序中。 I've looked all over but can't find a library/example code for this sort of menu.我已经查看了所有内容,但找不到此类菜单的库/示例代码。

Put simply, i want to...简而言之,我想...

  1. Display a menu显示菜单
  2. Allow the user to make a selection using the arrow keys [UP/DOWN]允许用户使用箭头键 [UP/DOWN] 进行选择
  3. Do different things when different options are selected [Enter]选择不同选项时做不同的事情 [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;
        }
    }

}

You could use the NuGet library called Spectre.Console .您可以使用名为Spectre.Console的 NuGet 库。

在此处输入图像描述

Or one of the libraries mentioned in this post:或者这篇文章中提到的库之一:

.Net library for console menu driven app 用于控制台菜单驱动应用程序的 .Net 库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在控制台应用程序中显示右键菜单? - How do I make the right click menu in a Console app appear? 如何在 C# 控制台应用程序中计算正确? - How do I calculate correct in C# Console App? 如何在控制台上显示此简单的C#应用​​程序? - How do I display this simple C# app to the console? C#如何将控制台应用程序添加到MonoGame项目 - C# How do I add a console app to a MonoGame project 如何在C#Interactive窗口中设置app.config中正常定义的开关? - How do I set switches normally defined in app.config in the C# Interactive window? 如何在C#中创建一个完全透明的winform,它是交互式的? - How do I create a fully transparent winform in C# that is interactive? 如何将简单的c#控制台应用程序移植到Windows应用商店(XAML)中? - How do I port a simple c# console app into a Windows Store app (XAML)? 如何创建一个 C# 应用程序来决定自己是显示为控制台还是窗口应用程序? - How do I create a C# app that decides itself whether to show as a console or windowed app? 如何在 C# 控制台应用程序中捕获 Ctrl+C (SIGINT)? - How do I trap Ctrl+C (SIGINT) in a C# console app? 如果用户在我的 C# 程序中打印“x”,我如何让控制台应用程序退出? - How can i make the console app Exit if the user prints 'x' in my C# program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM