Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

How to make a simple console select screen using C# ReadKey

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

}
Comment

PREVIOUS NEXT
Code Example
Csharp :: .net json result status code not working 
Csharp :: Remove access to admin from deleting the file in C# 
Csharp :: iterate through photon player gameobjects 
Csharp :: C# Linq item index 
Csharp :: select specific columns from datatable in c# using lambda 
Csharp :: sharepoint c# get list item query by lookup 
Csharp :: c sharp teleporting 
Csharp :: itext7 pdfwriter outputstream c# 
Csharp :: monegame deltatime 
Csharp :: asp c# page scroll position change after postback 
Csharp :: how to find the text position in excel in c# 
Csharp :: display array elemetns to text box c# 
Csharp :: C# foreach loop async but wait at end 
Csharp :: int model property shows 0 in textbox .net core 
Csharp :: how to print to printer in c# 
Csharp :: adding additional parameter to form submit 
Csharp :: change skybox color unity 
Csharp :: sto playing audiosource 
Csharp :: c# tell if a class is a child or the class itself 
Csharp :: unity sprite disappears when using transform.lookat 
Csharp :: raq query ef core 
Csharp :: Get a list of distinct values in List 
Csharp :: double quotes in a string c# 
Csharp :: run a command line from vb.net app 
Csharp :: c# insert backslash in string 
Csharp :: if or statement c# 
Csharp :: C# Console font 
Csharp :: c# switch example 
Csharp :: Transpose Matrix C Sharp 
Csharp :: or operator in c# 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =