Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple menu in a Console Application

Tags:

c#

I am trying to get my menu to repeat, so after selecting and doing option 1, it will got back to the menu and ask for another option to be chosen

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 contents taken out:

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 I have worded this poorly, so I hope that someone understands what I'm asking :P

like image 733
user2852418 Avatar asked Dec 04 '13 01:12

user2852418


People also ask

What is console application with example?

A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal, the command-line interface of some operating systems (Unix, DOS, etc.) or the text-based interface included with most graphical user interface (GUI) operating systems, such as the Windows Console ...

What is a console application in Visual Basic?

Visual Basic is a type-safe programming language that's designed to be easy to learn. A console app takes input and displays output in a command-line window, also known as a console. In this tutorial, you learn how to: Create a Visual Studio project.


2 Answers

There is a nuget package for this now

https://github.com/splttingatms/EasyConsole

Example

After adding the nuget package the Menu can be implemented the following way, This is a very Basic 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 the action you can place Any Method to run when selected

This will output something like this

  1. foo
  2. bar

Choose an option:

like image 118
Lord Darth Vader Avatar answered Oct 02 '22 14:10

Lord Darth Vader


See Console.ReadKey().

Replace your main function with:

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 until they press the Escape key. Each time you read the key, you can execute the action selected.

like image 23
NotMe Avatar answered Oct 02 '22 12:10

NotMe