Answers for "c# console menu system"

C#
1

c# console menu system

class Program
{
	static void Main(string[] args)
	{
    	bool showMenu = true;
        while (showMenu)
		{
        	showMenu = MainMenu();
		}
	}
    
	private static bool MainMenu()
    {
    	Console.Clear();
        Console.WriteLine("Choose an option:");
        Console.WriteLine("1) Reverse String");
        Console.WriteLine("2) Remove Whitespace");
        Console.WriteLine("3) Exit");
		Console.Write("\r\nSelect an option: ");
 
        switch (Console.ReadLine())
        {
        	case "1":
				ReverseString();
                return true;
			case "2":
            	RemoveWhitespace();
                return true;
			case "3":
            	return false;
			default:
            	return true;
		}
	}
 
	private static string CaptureInput()
    {
    	Console.Write("Enter the string you want to modify: ");
        return Console.ReadLine();
	}
 
    private static void ReverseString()
    {
    	Console.Clear();
        Console.WriteLine("Reverse String");
 
        char[] charArray = CaptureInput().ToCharArray();
        Array.Reverse(charArray);
        DisplayResult(String.Concat(charArray));
	}
 
    private static void RemoveWhitespace()
    {
    	Console.Clear();
        Console.WriteLine("Remove Whitespace");
 
		DisplayResult(CaptureInput().Replace(" ", ""));
	}
 
    private static void DisplayResult(string message)
    {
    	Console.WriteLine($"\r\nYour modified string is: {message}");
        Console.Write("\r\nPress Enter to return to Main Menu");
        Console.ReadLine();
	}
}
Posted by: Guest on July-24-2021
3

c# console application menu system

// This is a Visual Studio Console application.
// How to create a menu system!
// Make sure your using:
using System.Collections.Generic;

// Copy this into a class:
static int indexMainMenu = 0;

public static void mainMenu()
{
	Console.Clear();

	List<string> menuItems = new List<string>()
	{
    	"Play",
        "Settings",
        "Exit"
	};

    Console.CursorVisible = false;
    while (true)
    {
    	string selectedMenuItem = drawMainMenu(menuItems);
        if (selectedMenuItem == "Play")
		{
        	// Play the game, or start the app
		}
        else if (selectedMenuItem == "Settings")
        {
        	/* Go to settings. Call a method, in the method just
            copy this menu system and change the list names for
            each setting you want.*/
		}
        else if (selectedMenuItem == "Exit")
        {
        	Environment.Exit(0); // Or  System.Environment.Exit(0);
        }
	}
}

public static string drawMainMenu(List<string> items)
{
	for (int i = 0; i < items.Count; i++)
    {
    	if (i == indexMainMenu)
        {
        	Console.BackgroundColor = ConsoleColor.Gray;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine(items[i]);
        }
        else
        {
        	Console.WriteLine(items[i]);
        }
        Console.ResetColor();
	}

    ConsoleKeyInfo ckey = Console.ReadKey();
    if (ckey.Key == ConsoleKey.DownArrow)
    {
		if (indexMainMenu == items.Count - 1) { }
        else { indexMainMenu++; }
	}
    else if (ckey.Key == ConsoleKey.UpArrow)
    {
    	if (indexMainMenu <= 0) { }
        else { indexMainMenu--; }
    }
    else if (ckey.Key == ConsoleKey.LeftArrow)
    {
        Console.Clear();
    }
    else if (ckey.Key == ConsoleKey.RightArrow)
    {
		Console.Clear();
    }
    else if (ckey.Key == ConsoleKey.Enter)
    {
		return items[indexMainMenu];
    }
    else
    {
		return "";
    }

	Console.Clear();
    return "";
}

// wow you scrolled this far...
Posted by: Guest on September-03-2020

C# Answers by Framework

Browse Popular Code Answers by Language