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

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

Summary

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


Sample Program

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic, click Console Application, and then click OK.
  4. 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.
  5. 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
  6. 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("THIS IS FIRST LINE") ' Some text
    Console.WriteLine("THIS IS SECOND LINE") ' Some text
    Console.WriteLine("THIS IS THIRD LINE") ' Some text
    Console.WriteLine("THIS IS FOURTH LINE") ' Some text
    Console.WriteLine("THIS IS FIFTH LINE") ' Some text
    Console.WriteLine("Hit Enter to Clear") ' Some text
    Console.ReadLine() ' Wait for user input.
    ClearMyConsole.Clear() ' Clear the screen.
    Console.WriteLine("THE CONSOLE WAS CLEARED") ' Some text to clear console.
    Console.WriteLine("Hit Enter to Terminate") ' Some text
    Console.ReadLine() ' Wait for user input.
  7. Press F5 to run the program.

References

For more information about console functions, visit the following Microsoft Developer Network (MSDN) Web site:

Propriedades

ID do Artigo: 319239 - Última Revisão: 26 de nov de 2007 - Revisão: 1

Comentários