Want more features on Pastebin? Sign Up, it's FREE!
Guest

Menu.cs

By: a guest on Jul 28th, 2010  |  syntax: C#  |  size: 1.76 KB  |  views: 84  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleMenu
  5. {
  6.     enum MenuOptions { Play = 0, Credits, Exit }
  7.  
  8.     class Menu
  9.     {
  10.         public MenuOptions SelectedItem { get; set; }
  11.         public const string menuText = "{0,-3}Play\n{1,-3}Credits\n{2,-3}Exit";
  12.  
  13.         public Menu()
  14.         {
  15.             SelectedItem = MenuOptions.Play;
  16.         }
  17.  
  18.         public MenuOptions ShowMenu()
  19.         {
  20.             Console.CursorVisible = false;
  21.             ConsoleKeyInfo key;
  22.             SelectedItem = MenuOptions.Play;
  23.             do
  24.             {
  25.                 Console.Clear();
  26.                 Console.WriteLine(menuText,
  27.                     GetPrefix(SelectedItem == MenuOptions.Play),
  28.                     GetPrefix(SelectedItem == MenuOptions.Credits),
  29.                     GetPrefix(SelectedItem == MenuOptions.Exit));
  30.                 key = Console.ReadKey();
  31.                 if (key.Key == ConsoleKey.UpArrow)
  32.                     SelectedItem = MenuUp(SelectedItem);
  33.                 else if (key.Key == ConsoleKey.DownArrow)
  34.                     SelectedItem = MenuDown(SelectedItem);
  35.             } while (key.Key != ConsoleKey.Enter);
  36.             Console.CursorVisible = true;
  37.             return SelectedItem;
  38.         }
  39.  
  40.         public string GetPrefix(bool selected)
  41.         {
  42.             return selected ? ">" : "";
  43.         }
  44.  
  45.         public MenuOptions MenuUp(MenuOptions current)
  46.         {
  47.             if (current == MenuOptions.Play)
  48.                 return MenuOptions.Exit;
  49.             else return current - 1;
  50.         }
  51.  
  52.         public MenuOptions MenuDown(MenuOptions current)
  53.         {
  54.             if (current == MenuOptions.Exit)
  55.                 return MenuOptions.Play;
  56.             else return current + 1;
  57.         }
  58.     }
  59. }
clone this paste RAW Paste Data