So I've been working on a program that basically cures your boredom. (I wanted to show this program to the people at school when they use the computers).
The selection menu in the code is taken from this post:
C# Console app - How do I make an interactive menu?
But there is a bug (or more like my mistake) when you press enter on a selection, the screen clears and only refreshes when I press the up or down arrow key?
Here is the code
using System.Text;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
namespace Awesome_C__Project
{
internal class Program
{
public static List<Option> options;
static void Main(string[] args)
{
Encoding UTF8 = Encoding.UTF8;
Console.OutputEncoding = UTF8;
// Create options that you want your menu to have
options = new List<Option>
{
new Option(" Home ", () => HomePageLink()),
new Option(" Request ", () => RequestPageLink()),
new Option(" ⚙️ Configure ", () => ConfigurePageLink()),
new Option(" Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
// Set the default index of the selected item to be the first
int index = 0;
// Write the menu out
WriteMenu(options, options[index]);
// Store key info in here
ConsoleKeyInfo keyinfo;
do
{
keyinfo = Console.ReadKey();
// Handle each key input (down arrow will write the menu again with a different selected item)
if (keyinfo.Key == ConsoleKey.DownArrow)
{
if (index + 1 < options.Count)
{
index++;
WriteMenu(options, options[index]);
}
}
if (keyinfo.Key == ConsoleKey.UpArrow)
{
if (index - 1 >= 0)
{
index--;
WriteMenu(options, options[index]);
}
}
// Handle different action for the option
if (keyinfo.Key == ConsoleKey.Enter)
{
options[index].Selected.Invoke();
index = 0;
}
}
while (keyinfo.Key != ConsoleKey.X);
Console.ReadKey();
}
// Default action of all the options. You can create more methods
static void HomePageLink()
{
Console.Clear();
options = new List<Option>
{
new Option("> Home ", () => HomePageLink()),
new Option(" Request ", () => RequestPageLink()),
new Option(" ⚙️ Configure ", () => ConfigurePageLink()),
new Option(" Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
}
static void RequestPageLink()
{
Console.Clear();
options = new List<Option>
{
new Option(" Home ", () => HomePageLink()),
new Option("> Request ", () => RequestPageLink()),
new Option(" ⚙️ Configure ", () => ConfigurePageLink()),
new Option(" Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
}
static void UpdatePageLink()
{
Console.Clear();
options = new List<Option>
{
new Option(" Home ", () => HomePageLink()),
new Option(" Request ", () => RequestPageLink()),
new Option(" ⚙️ Configure ", () => ConfigurePageLink()),
new Option("> Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
}
static void ConfigurePageLink()
{
Console.Clear();
options = new List<Option>
{
new Option(" Home ", () => HomePageLink()),
new Option(" Request ", () => RequestPageLink()),
new Option("> ⚙️ Configure ", () => ConfigurePageLink()),
new Option(" Updates ", () => UpdatePageLink()),
new Option(" ❌ Exit ", () => Environment.Exit(0)),
};
}
static void WriteMenu(List<Option> options, Option selectedOption)
{
Console.Clear();
foreach (Option option in options)
{
if (option == selectedOption)
{
Console.Write("\u001b[48;2;50;50;50m");
}
else
{
Console.Write("\u001b[48;2;12;12;12m");
}
Console.WriteLine(option.Name);
}
}
}
public class Option
{
public string Name { get; }
public Action Selected { get; }
public Option(string name, Action selected)
{
Name = name;
Selected = selected;
}
}
}