Guest User

MenuSwitchClass

a guest
Feb 5th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ChangePageApp
  6. {
  7.     class PageUI
  8.     {
  9.             string[] array;
  10.             int pointer = 0;//указатель текущего пункта
  11.  
  12.         //конструктор в который передаешь пункты меню
  13.         public PageUI(params string[] puncts)
  14.         {
  15.             array = new string[puncts.Length];
  16.             for (int i = 0; i < puncts.Length; i++)
  17.             {
  18.                 array[i] = puncts[i];
  19.             }
  20.         }
  21.  
  22.         //функция для отображения меню и переключения
  23.            public int PageSwither()
  24.         {
  25.             ConsoleKeyInfo leverKey;
  26.  
  27.             //выводит в консоль все пункты меню, обновляется при каждом нажатии кнопок
  28.             do
  29.             {
  30.                 Console.Clear();
  31.                 for (int i = 0; i < array.Length; i++)
  32.                 {//если pointer указывает то выделяет
  33.                     if (pointer == i)
  34.                     {
  35.                         Console.ForegroundColor = ConsoleColor.Green;
  36.                         Console.WriteLine($">{array[i]}");
  37.                         Console.ResetColor();
  38.                     }
  39.                     //если pointer не указывает просто отображает
  40.                     else
  41.                     {
  42.                         Console.WriteLine(array[i]);
  43.                     }
  44.                 }
  45.  
  46.                 //переключает пункты меню
  47.                 leverKey = Console.ReadKey();
  48.                 if (leverKey.Key == ConsoleKey.DownArrow)
  49.                 {
  50.                     pointer++;
  51.                     if (pointer == array.Length)
  52.                     {
  53.                         pointer = 0;
  54.                     }
  55.                 }
  56.                 else if (leverKey.Key == ConsoleKey.UpArrow)
  57.                 {
  58.                     pointer--;
  59.                     if (pointer < 0)
  60.                     {
  61.                         pointer = array.Length - 1;
  62.                     }
  63.                 }
  64.                 else { Console.WriteLine("wrong input");break; }
  65.             } while (leverKey.Key != ConsoleKey.Enter);
  66.             //возвращает пункт который выбрал
  67.             return pointer;
  68.         }
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment