Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a console window clear the console window in VB .NET
DescriptionThis example shows how to make a console window clear the console window in VB .NET.
Keywordsconsole application, console window, clear, console, VB.NET
CategoriesVB.NET, Windows
 
See Knowledge Base article 319239: HOW TO: Clear the Console Window with Visual Basic .NET.

The ConsoleClearer class's Clear method clears the console. It gets a handle to stdout, gets information about the console screen buffer, fills the buffer with spaces, and then places the cursor in the upper left corner.

 
' 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

    ' Get Handle for standard output
    hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE)

    ' Get information about the standard output buffer of
    ' the Console
    GetConsoleScreenBufferInfo(hConsoleHandle, _
        strConsoleInfo)

    ' Fill output buffer with Empty characters (ASCII 32)
    FillConsoleOutputCharacter(hConsoleHandle, EMPTY, _
        strConsoleInfo.dwSize.X * strConsoleInfo.dwSize.Y, _
        strOriginalLocation, hWrittenChars)

    ' Set the Console cursor back to the origin
    SetConsoleCursorPosition(hConsoleHandle, _
        strOriginalLocation)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated