To create menu in C# (Sharp) Console + Code

By kedinn 2013/01/24

As creating menus in C# (Sharp)?

It is very important to know how to create a menu since its use is very frequent to have options of entrance by keyboard.

For that just they are beginning to always program they begin Soon with Application of Console and if you already dominate to program in console to happen to you to program in the application of Form Windows.

Here I leave two Examples them first is but simple and the second a little but complicated but nothing difficult, good we do not see:

First Example

I want to create a menu to make a simple calculator that executes the program and that leaves this screen:

An option chooses.

1º) Extreme

2º) Subtraction

3º) Multiplication

4º) Division

Example Code Menu in C# (Sharp)

They do not forget I convene must be within the Main but not always also you prune to use in several forms example within methods question to only practice:)

Console.Write (“1º) Extreme? + “\ n? + “2º) Subtraction? + “\ n? + “3º) Multiplication? + “\ n? + “4º) Division? + “\ n?);
            Console.Write (“Selects an option: ?);

            switch (Console.Read ())
            \ {
                ‘1’ marries: Console.Write (“Sum. ?);
                    //To continue logical and to extract //methods
                    break;
                it marries ‘2’: Console.Write (“Remains. ?);
                    //To continue logical and to extract //methods
                    break;
                it marries ‘3’: Console.Write (“Multiplication. ?);
                    //To continue logical and to extract //methods
                    break;
                it marries ‘4’: Console.Write (“Division. ?);
                    //To continue logical and to extract //methods
                    break;
            } 
            Console.ReadKey ();

 To see implemented code of the Calculator.

 Secondly example

another example to create otherwise the Menu in form ordinate, with color and but options.

Example Code Menu in C# (Sharp)

ConsoleKeyInfo op;

            do
            \ {
                Console.Clear (); //Limpiar the screen
                Console.WriteLine (“\ t \ t tMenú \ t \ - www.FOROSHTML5.org \ n?);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write (“[To] Agregar \ t?);
                Console.Write (“[and] Eliminar \ t?);
                Console.Write (“[B] Buscar \ t?);
                Console.Write (“[ESC] Salir \ t \ n \ n?);
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine (“It selects option…?);
                op = Console.ReadKey (true); //Que does not show the indicated key

                //m étodos is action, the properties are values
                switch (op. Key)
                \ {
                    ConsoleKey.A marries:
                        Console.WriteLine (“You selected the option To add?);
                        Console.Write (“It presses a key to continue…?);
                        Console.ReadKey ();
                        break;

                    ConsoleKey.E marries:
                        Console.WriteLine (“You selected the option To eliminate?);
                        Console.Write (“It presses a key to continue…?);
                        Console.ReadKey ();
                        break;

                    ConsoleKey.B marries:
                        Console.WriteLine (“You selected the option To look for?);
                        Console.Write (“It presses a key to continue…?);
                        Console.ReadKey ();
                        break;

                    it marries ConsoleKey.Escape:
                        Console.WriteLine (“Ciao?);

                        break;
                }
            } while (op. Key! = ConsoleKey.Escape);

Here the image of the second example:

menu in c# sharp