simple menu in c# console application tutorial code example
Example: c# console application menu system
using System.Collections.Generic;
static int indexMainMenu = 0;
public static void mainMenu()
{
Console.Clear();
List<string> menuItems = new List<string>()
{
"Play",
"Settings",
"Exit"
};
Console.CursorVisible = false;
while (true)
{
string selectedMenuItem = drawMainMenu(menuItems);
if (selectedMenuItem == "Play")
{
}
else if (selectedMenuItem == "Settings")
{
}
else if (selectedMenuItem == "Exit")
{
Environment.Exit(0);
}
}
}
public static string drawMainMenu(List<string> items)
{
for (int i = 0; i < items.Count; i++)
{
if (i == indexMainMenu)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine(items[i]);
}
else
{
Console.WriteLine(items[i]);
}
Console.ResetColor();
}
ConsoleKeyInfo ckey = Console.ReadKey();
if (ckey.Key == ConsoleKey.DownArrow)
{
if (indexMainMenu == items.Count - 1) { }
else { indexMainMenu++; }
}
else if (ckey.Key == ConsoleKey.UpArrow)
{
if (indexMainMenu <= 0) { }
else { indexMainMenu--; }
}
else if (ckey.Key == ConsoleKey.LeftArrow)
{
Console.Clear();
}
else if (ckey.Key == ConsoleKey.RightArrow)
{
Console.Clear();
}
else if (ckey.Key == ConsoleKey.Enter)
{
return items[indexMainMenu];
}
else
{
return "";
}
Console.Clear();
return "";
}