Simple menu in console application

I'm trying to get my menu back, so after selecting and executing option 1, it will return to the menu and ask you to choose another option

class Program { static void Main(string[] args) { FootballTeams MyCode = new FootballTeams(); MyCode.ListInit(); MyCode.DisplayMenu(); MyCode.AddTeams(); Console.ReadLine(); MyCode.ListInit(); MyCode.DisplayMenu(); MyCode.DisplayTeams(); Console.ReadLine(); MyCode.ListInit(); MyCode.DisplayMenu(); MyCode.Delete(); Console.ReadLine(); MyCode.ListInit(); MyCode.DisplayMenu(); MyCode.TeamSearch(); Console.ReadLine(); } } 

Here are the methods with the displayed content:

 class FootballTeams { public FootballTeams(){ } List<string> teams; public void ListInit() public void DisplayMenu() { Console.WriteLine("Football Manager"); Console.WriteLine(); Console.WriteLine("1. Add a Football team"); Console.WriteLine("2. List the Football teams"); Console.WriteLine("3. Search for a Football team"); Console.WriteLine("4. Delete a team"); Console.ReadLine(); } public void AddTeams() { Console.WriteLine("Enter a team to be added: "); string userinput = Console.ReadLine(); if (teams.Count < 10) { if (userinput != "Colchester") { teams.Add(userinput); foreach (var item in teams) Console.Write(item.ToString() + " "); } else Console.Write("NOT ALLOWED"); } else Console.Write("MAXIMUM LIMIT REACHED"); } public void DisplayTeams() { foreach(var item in teams) Console.Write(item.ToString() + " "); } public void TeamSearch() { Console.WriteLine("Please enter the team you wish to search for: "); string userinput = Console.ReadLine(); if (teams.Contains(userinput)) Console.WriteLine("Success, team " + userinput); } public void Delete() { Console.WriteLine("Enter a team you wish to delete: "); string userinput = Console.ReadLine(); teams.Remove(userinput); foreach (var item in teams) Console.Write(item.ToString() + " "); } 

I know this is poorly said, so I hope someone understands what I'm asking: P

+6
source share
4 answers

You can use the do while loop for this purpose. Make a small modification to your DispalyMenu () method and return a result similar to this.

 static public int DisplayMenu() { Console.WriteLine("Football Manager"); Console.WriteLine(); Console.WriteLine("1. Add a Football team"); Console.WriteLine("2. List the Football teams"); Console.WriteLine("3. Search for a Football team"); Console.WriteLine("4. Delete a team"); Console.WriteLine("5. Exit"); var result = Console.ReadLine(); return Convert.ToInt32(result); } 

and write this in the Main () method

 int userInput = 0; do { userInput = DisplayMenu(); }while(userInput!=5); 

So, until the user enters "5", the code will execute in a loop. Hope this helps.

+18
source

See: http://msdn.microsoft.com/en-us/library/471w8d85(v=vs.110).aspx

Replace the main function as follows:

 static void Main(string[] args) { FootballTeams MyCode = new FootballTeams(); MyCode.ListInit(); ConsoleKeyInfo cki; do { MyCode.DisplayMenu(); cki = Console.ReadKey(false); // show the key as you read it switch (cki.KeyChar.ToString()) { case "1": MyCode.AddTeams(); break; case "2": MyCode.DisplayTeams(); break; // etc.. } } while (cki.Key != ConsoleKey.Escape); } 

Essentially, you need to loop around until they hit the Escape key. Each time you read the key, you can perform the selected action.

+10
source

There is now a nuget package for this.

https://github.com/splttingatms/EasyConsole

Example

After adding the nuget package, the menu can be implemented as follows. This is a very simple option.

 static void Main(string[] args) { var menu = new EasyConsole.Menu() .Add("foo", () => Console.WriteLine("foo selected")) .Add("bar", () => Console.WriteLine("bar selected")); menu.Display(); } 

In action, you can put any method to start when you select

Something like this will be displayed here.

  • Foo
  • bar

Select an option:

+5
source

https://github.com/demcy/Console_Menu.git This is an easy starting version in C #

0
source

Source: https://habr.com/ru/post/959295/


All Articles