I want to make a simple menu in C# like : something like this should be printed out of console :
FirstOption
SecondOption
Exit
So far here is my code (there are problems with naming and encapsulation, but this is all just quick prototype, spent ~30 minutes):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Menu StartGame = new Menu("Start Game");
Menu EndGame = new Menu("End Game");
Console.WriteLine(StartGame);
Console.WriteLine(End Game);
EndGame.isChecked = false;
}
}
class Menu
{
private string Content;
public bool isChecked = true;
public Menu(string Content)
{
this.Content = Content;
}
public void CheckCondition()
{
if (isChecked)
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
}
else
{
Console.ResetColor();
}
}
public override string ToString()
{
this.CheckCondition();
return this.Content;
}
}
}
The idea is when a button is clicked the menu item is highlighted. When one come to the last menu item he can't press DownArrow again, the same for the first item and UpArrow.
I'm completely stuck with this.
I am not completely sure.. May be this could help you to get started.
while (true)
{
var ch = Console.ReadKey(false).Key;
switch (ch)
{
case ConsoleKey.UpArrow:
HighlightStartGame();
break;
case ConsoleKey.DownArrow:
HighlightEndGame();
break;
}
}
static void HighlightStartGame()
{
Console.Clear();
Console.ResetColor();
StartGame.isChecked = true;
Console.WriteLine(StartGame);
EndGame.isChecked = false;
Console.WriteLine(EndGame);
}
static void HighlightEndGame()
{
Console.Clear();
Console.ResetColor();
StartGame.isChecked = false;
Console.WriteLine(StartGame);
EndGame.isChecked = true;
Console.WriteLine(EndGame);
}
No you can't just do that because Win32 console doesn't support those methods. You can however use GDI to draw on the console window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With