C# Консольная псевдографика, создание меню, переключение между страницами
Формулировка задачи:
Добрый день, я немного ламер в C#, но у меня стоит задача:
Мне надо в консольном приложении создать меню(это осилил) и чтобы при нажатии на выбранном пункте переходило на нужную страницу(из-за ламерства сделал костыль), но при нажатии Esc - возвращало в меню(не осилил).
Смотрел примеры, которые приводили до меня - не совсем то, хотя один из них взял за основу, но переделать полностью под себя не знаю как..
Вот то что у меня сейчас в коде программы(он был скопирован из одного из примеров + чутка изменен):
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace test
- {
- class Program
- {
- public static void Print(string[] points, int choose)
- {
- Console.Clear();
- for (int i = 0; i < points.Length; i++)
- {
- Console.WriteLine("{0} {1}", points[i], i == choose ? "" + (char)9668 : "");
- }
- }
- public static int Menu(string[] points)
- {
- Console.CursorVisible = false; // Чтобы не было мигающего курсора.
- int choose = 0;
- while (true) // Бесконечный цикл.
- {
- Print(points, choose);
- switch (Console.ReadKey(true).Key)
- {
- case ConsoleKey.UpArrow: choose--; break;
- case ConsoleKey.DownArrow: choose++; break;
- case ConsoleKey.D: Console.CursorVisible = true; return -1;
- case ConsoleKey.Enter: Console.CursorVisible = true; return choose;
- }
- choose = (choose + points.Length) % points.Length; // Зацикливаем выбор.
- }
- }
- public static void Main()
- {
- string[] points = { "1) Выполнить ", "2) Настройка", "3) О программе", "4) Выход"};
- int choose = Menu(points);
- // Console.WriteLine(choose == -1 ? "Ничего не выбрано" : string.Format("Был выбран пункт №{0}", choose + 1));
- if (choose == 0) // Мой костыль. Пока думаю через if проверять что выбрано...
- {
- Console.Clear();
- Console.WriteLine("OK");
- Console.ReadKey();
- }
- }
- }
- }
Решение задачи: «C# Консольная псевдографика, создание меню, переключение между страницами»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- public static void Print(string[] points, int choose)
- {
- Console.Clear();
- for (int i = 0; i < points.Length; i++)
- {
- Console.WriteLine("{0} {1}", points[i], i == choose ? "" + (char)9668 : "");
- }
- }
- public static int MenuProcess(string[] points)
- {
- Console.CursorVisible = false;
- int choose = 0;
- while (true)
- {
- Print(points, choose);
- switch (Console.ReadKey(true).Key)
- {
- case ConsoleKey.UpArrow: choose--; break;
- case ConsoleKey.DownArrow: choose++; break;
- case ConsoleKey.D: Console.CursorVisible = true; return -1;
- case ConsoleKey.Enter: Console.CursorVisible = true; return choose;
- case ConsoleKey.Escape: return 0;
- }
- choose = (choose + points.Length) % points.Length;
- }
- }
- public static int GoToMenu()
- {
- string[] points = { "1) Выполнить ", "2) Настройка", "3) О программе", "4) Выход" };
- int result = MenuProcess(points);
- return result;
- }
- public static int GoToMainWindow()
- {
- Console.CursorVisible = false;
- Console.WriteLine("Try Esc for exit or Enter for menu page");
- while (true)
- {
- switch (Console.ReadKey(true).Key)
- {
- case ConsoleKey.Enter: return 1; // В меню
- case ConsoleKey.Escape: return 0; // Выход
- }
- }
- }
- public static void Loop(int sres)
- {
- int res = sres;
- bool loop = true;
- while (loop)
- {
- Console.Clear();
- if (res == 1)
- {
- res = GoToMainWindow();
- }
- if (res == 0)
- {
- Console.WriteLine("Bye");
- loop = false;
- }
- if (res == 1)
- {
- int menuRes;
- Console.Clear();
- menuRes = GoToMenu();
- if (menuRes == 0)
- res = 1;
- if (menuRes == 3) // 3 это "4) Выход"
- res = 0;
- }
- }
- }
- static void Main(string[] args)
- {
- Loop(1);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д