Advertisement
csaki

Console menu navigate with arrows

Jun 28th, 2013
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System;
  2. using System.Windows.Input;
  3.  
  4. namespace consoleMenuNavigate
  5. {
  6.     class Program
  7.     {
  8.         static string[] menu;
  9.         static int index = 0;
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             Initialize();
  14.             DrawMenu();
  15.  
  16.             while (true)
  17.             {
  18.                 GetKeyboardState();
  19.                 DrawMenu();
  20.             }
  21.         }
  22.  
  23.         static void Initialize()
  24.         {
  25.             menu = new string[] { "1. menüpont", "2. menüpont", "3. menüpont", "4. menüpont", "5. menüpont" };
  26.             // egyéb szükséges dolgok
  27.         }
  28.  
  29.         static void GetKeyboardState()
  30.         {
  31.             ConsoleKeyInfo info = Console.ReadKey();
  32.             if (info.Key == ConsoleKey.DownArrow)
  33.             {
  34.                 if (index < menu.Length - 1)
  35.                 {
  36.                     index++;
  37.                 }
  38.             }
  39.             if (info.Key == ConsoleKey.UpArrow)
  40.             {
  41.                 if (index > 0)
  42.                 {
  43.                     index--;
  44.                 }
  45.             }
  46.             if (info.Key == ConsoleKey.Enter)
  47.             {
  48.                 enterMenu(index);
  49.             }
  50.         }
  51.  
  52.         static void DrawMenu()
  53.         {
  54.             Console.SetCursorPosition(0, 0);
  55.             for (int i = 0; i < menu.Length; i++)
  56.             {
  57.                 if (i == index)
  58.                 {
  59.                     Console.BackgroundColor = ConsoleColor.Blue;
  60.                     Console.WriteLine(menu[i]);
  61.                 }
  62.                 else
  63.                 {
  64.                     Console.BackgroundColor = ConsoleColor.Black;
  65.                     Console.WriteLine(menu[i]);
  66.                 }
  67.             }
  68.             Console.BackgroundColor = ConsoleColor.Black;
  69.         }
  70.  
  71.         static void enterMenu(int index) // univerzalitás híján...
  72.         {
  73.             switch (index)
  74.             {
  75.                 case 1:
  76.  
  77.                     break;
  78.  
  79.                 case 2:
  80.  
  81.                     break;
  82.  
  83.                 case 3:
  84.  
  85.                     break;
  86.  
  87.                 case 4:
  88.  
  89.                     break;
  90.  
  91.                 case 5:
  92.  
  93.                     break;
  94.  
  95.                 default:
  96.  
  97.                     break;
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement