Paste the following sample code in the Class file (replace all of the existing code):
Imports System.Runtime.InteropServices
Public Class ClearConsole
Private Const STD_OUTPUT_HANDLE As Integer = &HFFFFFFF5
Private Const EMPTY As Byte = 32
' Structure defines the coordinates of a character cell in a console screen buffer.
' The origin of the coordinate system (0,0) is at the top-left cell of the buffer.
<StructLayout(LayoutKind.Sequential)> _
Structure COORD
Dim X As Short
Dim Y As Short
End Structure
' Structure defines the coordinates of the upper-left and lower-right corners of a rectangle
<StructLayout(LayoutKind.Sequential)> _
Structure SMALL_RECT
Dim Left As Short
Dim Top As Short
Dim Right As Short
Dim Bottom As Short
End Structure
' Structure containing information about the Console's screen buffer.
<StructLayout(LayoutKind.Sequential)> _
Structure CONSOLE_SCREEN_BUFFER_INFO
Dim dwSize As COORD
Dim dwCursorPosition As COORD
Dim wAttributes As Integer
Dim srWindow As SMALL_RECT
Dim dwMaximumWindowSize As COORD
End Structure
' Win32 API Function declarations.
Declare Auto Function GetStdHandle Lib "kernel32.dll" (ByVal nStdHandle As Integer) As IntPtr
Declare Auto Function FillConsoleOutputCharacter Lib "kernel32.dll" (ByVal hConsoleOutput As IntPtr, ByVal cCharacter As Byte, _
ByVal nLength As Integer, _
ByVal dwWriteCoord As COORD, _
ByRef lpNumberOfCharsWritten As IntPtr) As Integer
Declare Auto Function GetConsoleScreenBufferInfo Lib "kernel32.dll" (ByVal hConsoleOutput As IntPtr, _
ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
Declare Auto Function SetConsoleCursorPosition Lib "kernel32.dll" (ByVal hConsoleOutput As IntPtr, ByVal dwCursorPosition As COORD) As Integer
' Subroutine used to clear the Console screen.
Public Sub Clear()
Dim hConsoleHandle As IntPtr
Dim hWrittenChars As IntPtr
Dim strConsoleInfo As CONSOLE_SCREEN_BUFFER_INFO
Dim strOriginalLocation As COORD
hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE) ' Get Handle for standard output
GetConsoleScreenBufferInfo(hConsoleHandle, strConsoleInfo) ' Get information about the standard output buffer of the Console
FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.X * strConsoleInfo.dwSize.Y, strOriginalLocation, hWrittenChars) ' Fill output buffer with Empty characters (ASCII 32)
SetConsoleCursorPosition(hConsoleHandle, strOriginalLocation) ' Set the Console cursor back to the origin
End Sub
End Class