Code template for a menu in a console application.

ddanbe 2 Tallied Votes 5K Views Share

Short piece of code to add a "menu" to your Console application.

Momerath commented: Coolio +13
charlybones commented: simple, effective, great for new students. +2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Change to your number of menuitems.
            const int maxMenuItems = 3;

            int selector = 0;
            bool good = false;

            while (selector != maxMenuItems)
            {
                Console.Clear(); 
                DrawTitle();
                DrawMenu(maxMenuItems);
                good = int.TryParse(Console.ReadLine(), out selector);
                if (good)
                {
                    switch (selector)
                    {
                        case 1:
                            Console.WriteLine("// code for case 1 here");
                            break;
                        case 2:
                            Console.WriteLine("// code for case 2 here");
                            break;
                        // possibly more cases here
                        default:
                            if (selector != maxMenuItems)
                            {
                                ErrorMessage();
                            }
                            break;
                    }
                }
                else
                {
                    ErrorMessage();
                }
                Console.ReadKey();
            }
        }

        private static void ErrorMessage()
        {
            Console.WriteLine("Typing error, press key to continue.");
        }

        private static void DrawStarLine()
        {
            Console.WriteLine("************************");
        }

        private static void DrawTitle()
        {
            DrawStarLine();
            Console.WriteLine("+++   MYTITLE HERE   +++");
            DrawStarLine();
        }

        private static void DrawMenu(int maxitems)
        {
            DrawStarLine();
            Console.WriteLine(" 1. MenuItem One");
            Console.WriteLine(" 2. MenuItem Two");
            // more here
            Console.WriteLine(" 3. Exit program");
            DrawStarLine();
            Console.WriteLine("Make your choice: type 1, 2,... or {0} for exit", maxitems);
            DrawStarLine();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.