C# コンソールの選択式メニュー

using System;
using System.Collections.Generic;
using System.Linq;

namespace SampleConsole
{
    public class Calendar
    {
        public static int DisplayMenu(string prompt, List<string> options)
        {
            int selectedIndex = 0;

            ConsoleKey keyPressed;
            do
            {
                Console.Clear();
                if (!string.IsNullOrWhiteSpace(prompt))
                {
                    Console.WriteLine(prompt);
                }

                for (int i = 0; i < options.Count; i++)
                {
                    if (i == selectedIndex)
                        Console.WriteLine($"> {options[i]} <");  // 選択中のオプションを強調表示
                    else
                        Console.WriteLine($"  {options[i]}");
                }

                keyPressed = Console.ReadKey().Key;
                // 上下の矢印キーに基づいて選択されたインデックスを更新
                switch (keyPressed)
                {
                    case ConsoleKey.UpArrow:
                        selectedIndex = (selectedIndex - 1 + options.Count) % options.Count;
                        break;
                    case ConsoleKey.DownArrow:
                        selectedIndex = (selectedIndex + 1) % options.Count;
                        break;
                }
            } while (keyPressed != ConsoleKey.Enter);  // Enterキーが押されるまでループ

            return selectedIndex;  // 選択されたオプションのインデックスを返す
        }


        public static void Main(string[] args)
        {
            List<string> menuOptions = new List<string> { "Option 1", "Option 2", "Option 3", "Exit" };
            int selectedOption = DisplayMenu("Please choose an option:", menuOptions);

            switch (selectedOption)
            {
                case 0:
                    Console.WriteLine("Option 1 selected");
                    break;
                case 1:
                    Console.WriteLine("Option 2 selected");
                    break;
                case 2:
                    Console.WriteLine("Option 3 selected");
                    break;
                case 3:
                    Console.WriteLine("Exiting...");
                    break;
            }

        }
    }
} 

具体的に画面が変わるように見えるのは、画面をクリアにしてから
違う文字列を表示しているため。

using System;
using System.Collections.Generic;
using System.Linq;

namespace SampleConsole
{
    public class Calendar
    {
        public static int DisplayMenu()
        {
            int selectedIndex = 0;
            bool selected = false;
            List<string> options = new List<string> { "Option 1", "Option 2", "Option 3", "Exit" };

            while (!selected)
            {
                Console.Clear();

                for (int i = 0; i < options.Count; i++)
                {
                    if (i == selectedIndex)
                        Console.WriteLine($"> {options[i]} <");
                    else
                        Console.WriteLine($"  {options[i]}");
                }

                ConsoleKey keyPressed = Console.ReadKey().Key;

                switch (keyPressed)
                {
                    case ConsoleKey.UpArrow:
                        selectedIndex = (selectedIndex - 1 + options.Count) % options.Count;
                        break;
                    case ConsoleKey.DownArrow:
                        selectedIndex = (selectedIndex + 1) % options.Count;
                        break;
                    case ConsoleKey.Enter:
                        selected = true;
                        switch (selectedIndex)
                        {
                            case 0:
                                Console.WriteLine("Option 1 selected");
                                break;
                            case 1:
                                Console.WriteLine("Option 2 selected");
                                break;
                            case 2:
                                Console.WriteLine("Option 3 selected");
                                break;
                            case 3:
                                Console.WriteLine("Exiting...");
                                break;
                        }
                        break;
                }
            }

            return selectedIndex;
        }

        public static void Main(string[] args)
        {

            int selectedOption = DisplayMenu();

        }
    }
}


この記事が気に入ったらサポートをしてみませんか?