简体   繁体   中英

Making simple console menu in c#

I want to make a simple menu in C# like : something like this should be printed out of console :

FirstOption 
SecondOption
Exit

So far here is my code (there are problems with naming and encapsulation, but this is all just quick prototype, spent ~30 minutes):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Menu StartGame = new Menu("Start Game");
            Menu EndGame = new Menu("End Game");
            Console.WriteLine(StartGame);
            Console.WriteLine(End Game);
            EndGame.isChecked = false;
        }
    }

    class Menu
    {
        private string Content;
        public bool isChecked = true;

        public Menu(string Content)
        {
            this.Content = Content;
        }
        public void CheckCondition()
        {
            if (isChecked)
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Black;
            }
            else
            {
                Console.ResetColor();
            }
        }
        public override string ToString()
        {
            this.CheckCondition();
            return this.Content;
        }
    }
}

The idea is when a button is clicked the menu item is highlighted. When one come to the last menu item he can't press DownArrow again, the same for the first item and UpArrow .

I'm completely stuck with this.

I am not completely sure.. May be this could help you to get started.

while (true)
        {
            var ch = Console.ReadKey(false).Key;
            switch (ch)
            {
                case ConsoleKey.UpArrow:
                    HighlightStartGame();
                    break;

                case ConsoleKey.DownArrow:
                    HighlightEndGame();
                    break;
            }
        }
static void HighlightStartGame()
    {
        Console.Clear();
        Console.ResetColor();
        StartGame.isChecked = true;
        Console.WriteLine(StartGame);
        EndGame.isChecked = false;
        Console.WriteLine(EndGame);

    }

    static void HighlightEndGame()
    {
        Console.Clear();
        Console.ResetColor();
        StartGame.isChecked = false;
        Console.WriteLine(StartGame);
        EndGame.isChecked = true;
        Console.WriteLine(EndGame);

    }

No you can't just do that because Win32 console doesn't support those methods. You can however use GDI to draw on the console window.

The problem is that the console cannot process any mouse events. How do you want to click on the menu? You will have to do everything with keys. The options you have are to either define keystrokes (like Ctrl-F or Alt-F for "FirstEntry") in order to activate menu entries, or to implement a navigation with arrow keys, allowing you to move around fields (button or menu fields and text fields). This is not built in, so you will have to do everything in code. You will have to use the SetCursorPosition and the ReadKey methods of the console in order to achieve this. I remember having done this on a VT100 terminal eons ago.

I've written a console menu library for C# . It has no mouse support, but it might still be a starting point for you?

CMenu is a lightweight, low-ceremony framework for building console menus in .Net. Instead of manually prompting the user for input and parsing it, you define commands in a short, structured and comprehensive way, and let CMenu handle the rest.

CMenu aims for low overhead - simple stuff should be simple to implement. If you don't use a feature, you don't need to know anything about it.

At the same time, complex scenarios are supported. Large menus can easily be split into several classes. Background self-configuration. You do not have to worry about all the annoying details involved in larger menus, it will just work.

Most importantly, it is very simple and fast to use. Commands can be abbreviated, a smart parser enables even partial matching. A help command is integrated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM