【问题标题】:C# Console app - How do I make an interactive menu?C# 控制台应用程序 - 如何制作交互式菜单?
【发布时间】:2020-07-01 05:19:04
【问题描述】:

我正在尝试制作一个类似于以下内容的菜单:

> Thing
  Another Thing
  Yet Another Thing
  Exit

在 C# 控制台应用程序中。 我已经翻遍了,但找不到此类菜单的库/示例代码。

简单地说,我想...

  1. 显示菜单
  2. 允许用户使用箭头键 [UP/DOWN] 进行选择
  3. 选择不同选项时执行不同操作 [Enter]

【问题讨论】:

  • 对于 2 和 3:stackoverflow.com/questions/7280591/… 如果用户按上/下键,您必须重绘控制台屏幕。
  • 您可能需要查看 gui.cs 或 C-sharp-console-gui-framework 以获取交互式 gui。

标签: c# user-interface menu console-application


【解决方案1】:

主要是能够捕获输入。控制台只能通过清除控制台窗口并重新渲染它来“模拟”交互式菜单。

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;
        }
    }

}

【讨论】:

  • 谢谢,我应该可以修改它以适应我正在尝试做的事情。
猜你喜欢
  • 2016-04-30
  • 2012-04-08
  • 2011-11-24
  • 2012-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多