Microsoft KB Archive/319239

= How to clear the Console window with Visual Basic .NET or Visual Basic 2005 =

Article ID: 319239

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q319239



For a Microsoft Visual C# version of this article, see 319257.



IN THIS TASK
SUMMARY
 * Sample Program

REFERENCES



SUMMARY
This step-by-step article demonstrates how to clear the Console window programmatically by using Visual Basic .NET or Visual Basic 2005.

back to the top

Sample Program
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual Basic, click Console Application, and then click OK. On the Project menu, click Add Class to open the Add New Item dialog box. Verify that Class is selected, and then click Open to accept the default name for the file.

Note In Visual Studio 2005, click Add instead of Open.  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.  _ 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  _ 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 &quot;kernel32.dll&quot; (ByVal nStdHandle As Integer) As IntPtr Declare Auto Function FillConsoleOutputCharacter Lib &quot;kernel32.dll&quot; (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 &quot;kernel32.dll&quot; (ByVal hConsoleOutput As IntPtr, _                                                                      ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer Declare Auto Function SetConsoleCursorPosition Lib &quot;kernel32.dll&quot; (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 </li>  Add the following code to the Sub Main section of the Console application: Dim ClearMyConsole As New ClearConsole ' Start an instance of class. Console.WriteLine(&quot;THIS IS FIRST LINE&quot;) ' Some text Console.WriteLine(&quot;THIS IS SECOND LINE&quot;) ' Some text Console.WriteLine(&quot;THIS IS THIRD LINE&quot;) ' Some text Console.WriteLine(&quot;THIS IS FOURTH LINE&quot;) ' Some text Console.WriteLine(&quot;THIS IS FIFTH LINE&quot;) ' Some text Console.WriteLine(&quot;Hit Enter to Clear&quot;) ' Some text Console.ReadLine ' Wait for user input. ClearMyConsole.Clear ' Clear the screen. Console.WriteLine(&quot;THE CONSOLE WAS CLEARED&quot;) ' Some text to clear console. Console.WriteLine(&quot;Hit Enter to Terminate&quot;) ' Some text Console.ReadLine ' Wait for user input. </li> Press F5 to run the program.</li></ol>

back to the top

<div class="references_section">