Module Main
#Region " Variables "
''' <summary>
''' The chooseable menu items.
''' </summary>
Private ReadOnly MenuItems() As String =
{
"[1] One",
"[2] Two",
"[3] Three",
"[4] Four",
"[H] Help",
"[X] Exit"
}
''' <summary>
''' Indicates the selected menu item.
''' </summary>
Private CurrentMenuItem As Short = 0
''' <summary>
''' The object to read in a pressed key.
''' </summary>
Private key As ConsoleKeyInfo = Nothing
''' <summary>
''' Indicates wether the user pressed a valid key.
''' </summary>
Private ValidKeyIsPressed As Boolean = False
''' <summary>
''' To manage known errors.
''' </summary>
Private ReadOnly KnownErrors
As New Dictionary(Of Integer,
String) From
{
{1, "Wrong parameter"},
{2, "Parameter not specified"},
{9999, "Unknown Error"}
}
#End Region
#Region " Main Procedure "
''' <summary>
''' The main procedure of this application.
''' </summary>
Public Sub Main()
Console.Title = HelpSection.Title ' Set the Console Title.
ShowMenu() ' Display the console menu.
DoSomething()
Environment.Exit(0) ' Exit succesfully.
End Sub
#End Region
#Region " Methods "
''' <summary>
''' Displays the console menu.
''' </summary>
Private Sub ShowMenu()
' Wait for a valid key.
Do Until ValidKeyIsPressed
' Clear the screen.
Console.Clear()
' Print the application Logotype.
Write_Colored_Text(HelpSection.Logo, True, ConsoleColor.Green) ' Print the Logo.
' Print a simple help text.
Write_Colored_Text("[+] Choose an option:", True, ConsoleColor.White)
Console.WriteLine()
' Loop through all of the menu items to print them.
For Each MenuItem As String In MenuItems
If MenuItem = MenuItems.ElementAt(CurrentMenuItem) Then
Write_Colored_Text(String.Format(" -> {0}", MenuItem), True, ConsoleColor.Green)
Else
Write_Colored_Text(MenuItem, True, ConsoleColor.Gray)
End If
Next MenuItem
' Waits until the user presses a key.
key = Console.ReadKey(True)
' Process the pressed key.
Select Case key.Key
Case Keys.Down ' If the key the user pressed is a "DownArrow", the current menu item will deacrease.
CurrentMenuItem += 1
If CurrentMenuItem > MenuItems.Length - 1 Then
CurrentMenuItem = 0
End If
Case Keys.Up ' If the key the user pressed is a "UpArrow", the current menu item will increase.
CurrentMenuItem -= 1
If CurrentMenuItem < 0 Then
CurrentMenuItem = CShort(MenuItems.Length - 1)
End If
Case Keys.Enter ' Enter Key (Select current menu item).
ValidKeyIsPressed = True
Case Keys.D1 To Keys.D4 ' A numeric key is pressed between 1-4.
CurrentMenuItem = key.Key.ToString.Substring(1) - 1
ValidKeyIsPressed = True
Case Keys.H ' H Key (Print Help).
PrintHelp()
ValidKeyIsPressed = True
Case Keys.X ' X Key (Exit from application).
Environment.Exit(0)
End Select
Loop
End Sub
''' <summary>
''' Do something.
''' </summary>
Private Sub DoSomething()
Console.WriteLine()
Write_Colored_Text("User Pressed Key: " & key.Key.ToString, True, ConsoleColor.Red, ConsoleColor.White)
Write_Colored_Text("Selected Option : " & MenuItems(CurrentMenuItem), True, ConsoleColor.Red, ConsoleColor.White)
Write_Colored_Text("Selected Index : " & CurrentMenuItem.ToString, True, ConsoleColor.Red, ConsoleColor.White)
Console.ReadLine()
End Sub
#End Region
#Region " Miscellaneous Methods "
''' <summary>
''' Writes colored text on the Console.
''' </summary>
''' <param name="Text">Indicates the text to write.</param>
''' <param name="AddNewLine">Adds an empty line at the end of the text.</param>
''' <param name="ForeColor">Indicates the text color.</param>
''' <param name="BackColor">Indicates the background color.</param>
Private Sub Write_Colored_Text(ByVal Text As String,
Optional ByVal AddNewLine As Boolean = False,
Optional ByVal ForeColor As ConsoleColor = Nothing,
Optional ByVal BackColor As ConsoleColor = Nothing)
Dim CurrentForegroundColor As ConsoleColor = Console.ForegroundColor
Dim CurrentBackgroundColor As ConsoleColor = Console.BackgroundColor
Console.ForegroundColor = If(ForeColor = Nothing, CurrentForegroundColor, ForeColor)
Console.BackgroundColor = If(ForeColor = Nothing, CurrentBackgroundColor, BackColor)
If AddNewLine Then
Console.WriteLine(Text)
Else
Console.Write(Text)
End If
Console.ForegroundColor = CurrentForegroundColor
Console.BackgroundColor = CurrentBackgroundColor
End Sub
''' <summary>
''' Set the window state of the console.
''' </summary>
''' <param name="WindowState">Indicates the new state of the console window.</param>
Public Function SetWindow(ByVal WindowState As ConsoleWindowState.WindowState) As Boolean
Return ConsoleWindowState.SetWindowState(WindowState)
End Function
''' <summary>
''' Print the Help section and exits from the application.
''' </summary>
Private Sub PrintHelp()
Console.WriteLine(HelpSection.Help)
Environment.Exit(0) ' Exit succesfully.
End Sub
''' <summary>
''' Launch a known error and exits from the application.
''' </summary>
''' <param name="ErrorID">
''' Indicates the known Error Identifier.
''' This value is also used to specify the ExitCode.
''' </param>
''' <param name="MoreInfo">Indicates additional information.</param>
Private Sub LaunchError(ByVal ErrorID As Integer, Optional ByVal MoreInfo As String = Nothing)
Console.WriteLine()
Write_Colored_Text(KnownErrors(ErrorID) &
If(MoreInfo IsNot Nothing, ": " & MoreInfo, Nothing),
True, ConsoleColor.White, ConsoleColor.DarkRed)
Environment.Exit(ErrorID) ' Exit with error exitcode.
End Sub
#End Region
End Module