C#C
C#14mo ago
ClownAcademy

Menu Validation 'char input'

I just asking for some general directions to design a solid input validation for a menu.
It 'works' but I feel like I am using the wrong tools.
I want it to only use a key press, that is fine. The overall come I wrote doesn't feel solid.
        static void Main()
        {
            List<string> fruitList = new List<string>();
            bool IsMenuActive = true;

            while (IsMenuActive)
            {
                //Console.WriteLine("MainMenu\\");
                Console.WriteLine("[1] - Menu1");
                Console.WriteLine("[2] - Menu2");
                Console.WriteLine("[3] - Menu3");
                Console.WriteLine("[4] - Menu4");
                Console.WriteLine();
                ValidKeyPress();
            }




            static char ValidKeyPress()
            {
                
                Console.Write("Choose menu: ");
                char userInput = Console.ReadKey().KeyChar;
                while (!char.IsDigit(userInput))
                {
                    Console.WriteLine();
                    Console.Write("Wrong keypress, try again: ");
                    userInput = Console.ReadKey(true).KeyChar;
                    Console.Write(userInput);
                }

                Console.WriteLine();
                return userInput;



            }
Was this page helpful?