Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit cea2d56

Browse files
author
Thomas
committed
Initial codebase
I wrote this before putting it into a git repository...
1 parent fe66961 commit cea2d56

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

ConsoleMenu.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29920.165
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleMenu", "ConsoleMenu\ConsoleMenu.csproj", "{4C4A8789-483E-407F-AB62-C44118BBD43A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{4C4A8789-483E-407F-AB62-C44118BBD43A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4C4A8789-483E-407F-AB62-C44118BBD43A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4C4A8789-483E-407F-AB62-C44118BBD43A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4C4A8789-483E-407F-AB62-C44118BBD43A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {A0F7BEE5-E727-4F92-B027-66BD11BEEBE4}
24+
EndGlobalSection
25+
EndGlobal

ConsoleMenu/ConsoleMenu.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>

ConsoleMenu/Menu.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ConsoleMenu
8+
{
9+
class Menu
10+
{
11+
public string[] menuTextItems { get; set; }
12+
public string menuTitle { get; set; }
13+
14+
public Menu(string[] menuItems, string title)
15+
{
16+
menuTextItems = menuItems;
17+
menuTitle = title;
18+
}
19+
20+
public int displayMenu()
21+
{
22+
MenuItem[] menuItems = new MenuItem[menuTextItems.Length];
23+
24+
for (int i = 0; i < menuItems.Length; i++)
25+
{
26+
menuItems[i] = new MenuItem(menuTextItems[i]);
27+
}
28+
29+
menuItems[0].isSelected = true;
30+
int currentSelection = 0;
31+
32+
while (true)
33+
{
34+
Console.CursorLeft = 0;
35+
Console.CursorTop = 0;
36+
Console.CursorVisible = false;
37+
38+
foreach (MenuItem item in menuItems)
39+
{
40+
if (!item.isSelected)
41+
{
42+
item.foregroundColor = ConsoleColor.White;
43+
item.backgroundColor = ConsoleColor.Black;
44+
}
45+
else
46+
{
47+
item.foregroundColor = ConsoleColor.Black;
48+
item.backgroundColor = ConsoleColor.White;
49+
item.isSelected = false;
50+
}
51+
52+
}
53+
54+
Console.ForegroundColor = ConsoleColor.Magenta;
55+
Console.BackgroundColor = ConsoleColor.Black;
56+
57+
Console.WriteLine(menuTitle);
58+
59+
foreach (MenuItem item in menuItems)
60+
{
61+
Console.ForegroundColor = item.foregroundColor;
62+
Console.BackgroundColor = item.backgroundColor;
63+
Console.WriteLine(item.text);
64+
}
65+
66+
ConsoleKeyInfo keyInfo = Console.ReadKey();
67+
ConsoleKey key = keyInfo.Key;
68+
69+
if (key == ConsoleKey.UpArrow && currentSelection != 0)
70+
{
71+
currentSelection--;
72+
73+
menuItems[currentSelection].isSelected = true;
74+
}
75+
else if (key == ConsoleKey.DownArrow && currentSelection != (menuItems.Length) - 1)
76+
{
77+
currentSelection++;
78+
79+
menuItems[currentSelection].isSelected = true;
80+
}
81+
else if (key == ConsoleKey.Enter)
82+
{
83+
Console.ForegroundColor = ConsoleColor.White;
84+
Console.BackgroundColor = ConsoleColor.Black;
85+
86+
Console.Clear();
87+
88+
return currentSelection;
89+
}
90+
}
91+
}
92+
}
93+
}

ConsoleMenu/MenuItem.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ConsoleMenu
6+
{
7+
class MenuItem
8+
{
9+
public ConsoleColor foregroundColor { get; set; }
10+
public ConsoleColor backgroundColor { get; set; }
11+
public string text { get; set; }
12+
public bool isSelected { get; set; }
13+
14+
public MenuItem(string itemText)
15+
{
16+
text = itemText;
17+
this.foregroundColor = ConsoleColor.White;
18+
this.backgroundColor = ConsoleColor.Black;
19+
this.isSelected = false;
20+
}
21+
}

0 commit comments

Comments
 (0)