Home > OS >  How to go to main menu from submenu in c# console app
How to go to main menu from submenu in c# console app

Time:10-31

I have below menu options in my c# console app.

Menu

When user selects option 2 then user can see products in pagination format. I need to go to main menu again once user enter 0. I am not sure how can I do that as its console app. Once user done with products pagination thing, he should able to return to maain menu. There I am stuck.

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    var shouldRun = true;
    DisplayOptions();

    while (shouldRun )
    {
        Console.Write("Enter an option: ");
        var input = Console.ReadKey();
        Console.WriteLine("\n");
        ConsoleKeyInfo selectedCurrency ;
        var currencyName = "USD";

        switch (input.Key)
        {
            case ConsoleKey.NumPad1:
            case ConsoleKey.D1:
                Console.WriteLine("Printing all products");
                OutputAllProduct(currencyName);
                break;
            case ConsoleKey.NumPad2:
            case ConsoleKey.D2:
                Console.WriteLine("Printing paginated products");
                PaginatedProducts();
                break;                           
            case ConsoleKey.Q:
                shouldRun = false;
                break;
            default:
                Console.WriteLine("Invalid option!");
                break;
        }

        Console.WriteLine();
        DisplayOptions();
    }

    Console.Write("\n\rPress any key to exit!");
    Console.ReadKey();

   await _host.StopAsync(stoppingToken);
}
public void OutputPaginatedProducts()
        {
            int totalPages = products.Count() / 5;
            do
            {
                Console.Write($"Please Enter Page number from 1 to {totalPages} or press 0 to exit\n");
                int pageNumber;

                if (int.TryParse(Console.ReadLine(), out pageNumber))
                {
                    if (pageNumber >= 1 && pageNumber <= totalPages)
                    {

                        IEnumerable<Product> result = products.Skip((pageNumber - 1) * 5).Take(5);
                        Console.WriteLine();
                        Console.WriteLine("Displaying page: "   pageNumber);
                        foreach (Product product in result)
                        {
                            Console.WriteLine($"ID : {product.Id}\t Product Name : {product.ProductName}\tPrice : {product.Price} \tCurrency : {product.Price} {Currency.USD}");
                        }
                        Console.WriteLine();
                    }

                    else if(pageNumber==0)
                    {
                        Environment.Exit(0);
                       
                    }
                }
                else
                {
                    Console.WriteLine($"Page number must be integer between 1 and {totalPages}");
                }
            } while (1==1);
        }

CodePudding user response:

Just let the method return.

Instead of calling Environment.Exit(0); You need to put a break or return there

  • Related