久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C# 控制臺應用程序 - 如何制作交互式菜單?

C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
本文介紹了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.

簡單來說,我想...

  1. 顯示菜單
  2. 允許用戶使用箭頭鍵 [UP/DOWN] 進行選擇
  3. 選擇不同的選項時做不同的事情[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# 控制臺應用程序 - 如何制作交互式菜單?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
How to add an icon to System.Windows.Forms.MenuItem?(如何向 System.Windows.Forms.MenuItem 添加圖標?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
Building a database driven menu with ASP.NET, JQuery and Suckerfish(使用 ASP.NET、JQuery 和 Suckerfish 構建數據庫驅動的菜單)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應用程序并單擊菜單條)
主站蜘蛛池模板: 日韩欧美在线免费观看视频 | h视频免费在线观看 | 国产精品久久久久无码av | 91免费福利视频 | 欧美一区免费 | 99视频免费播放 | 欧美一区二区三区四区视频 | 久热免费在线 | 国产一区二区三区在线免费观看 | 激情六月天 | 午夜国产 | 国产欧美一区二区三区在线播放 | 欧产日产国产精品99 | 中文在线а√在线8 | 色久电影| 一区二区三区av夏目彩春 | 精品国产综合 | 久久免费高清视频 | 九九亚洲精品 | 亚洲成av人影片在线观看 | 久草网免费 | 亚洲毛片在线 | 黑人精品xxx一区一二区 | 偷拍第一页 | 人人爽日日躁夜夜躁尤物 | 久久综合激情 | 精品久久久久一区二区国产 | 国产精品亚洲第一区在线暖暖韩国 | 国产精品自产拍 | 黄色片免费看 | 91免费入口| 99久久免费观看 | 亚洲综合色丁香婷婷六月图片 | 97起碰| 成人免费观看男女羞羞视频 | 天堂男人av | 97超碰站 | 久久大陆| 国产成人影院 | 在线观看视频91 | 日本三级电影在线免费观看 |