Function for Clear Console

  • Thread starter Thread starter sebplays
  • Start date Start date
S

sebplays

Hi,

I seach function for clear screen in VB.Net for console
application.

Please help me
 
sebplays said:
Hi,

I seach function for clear screen in VB.Net for console
application.

There isn't one. You'd need to go to unmanaged code and the Windows APIs.
 
The Console class does not have a method for this. You would have to write a
wrapper for this and use the Windows API as far as I can tell.

Regards OHM
 
-----Original Message-----


There isn't one. You'd need to go to unmanaged code and the Windows APIs.


.


Please send me exemple
what do you do for cleaning screen in VB for application
console.
 
Please send me exemple
what do you do for cleaning screen in VB for application
console.


Option Strict On
Option Explicit On

Imports System.Runtime.InteropServices

Module Module1

Sub Main()
Console.WriteLine("A line of text....")

Console.ReadLine()

CLS()

Console.WriteLine("A new line of text...")
End Sub

' adapted from the msdn example:
' http://msdn.microsoft.com/library/d...ry/en-us/dllproc/base/clearing_the_screen.asp
Public Sub CLS()
Dim hOutput As IntPtr = GetStdHandle(HandleType.Output)
Dim coordScreen As New COORD(0, 0)
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
Dim dwConSize As Integer
Dim cWritten As Integer

GetConsoleScreenBufferInfo(hOutput, csbi)
dwConSize = csbi.dwSize.X * csbi.dwSize.Y
FillConsoleOutputCharacter( _
hOutput, " "c, dwConSize, coordScreen, cWritten)
FillConsoleOutputAttribute( _
hOutput, csbi.wAttributes, dwConSize, coordScreen, cWritten)
SetConsoleCursorPosition(hOutput, coordScreen)
End Sub

Private Enum HandleType
Input = -10
Output = -11
[Error] = -12
End Enum

<StructLayout(LayoutKind.Sequential)> _
Private Structure COORD
Public X As Short
Public Y As Short

Public Sub New(ByVal X As Short, ByVal Y As Short)
Me.X = X
Me.Y = Y
End Sub
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure SMALL_RECT
Public Left As Short
Public Top As Short
Public Right As Short
Public Bottom As Short
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure CONSOLE_SCREEN_BUFFER_INFO
Public dwSize As COORD
Public dwCursorPosition As COORD
Public wAttributes As Short
Public srWindow As SMALL_RECT
Public dwMaximumWindowSize As COORD
End Structure

Private Declare Function GetStdHandle Lib "kernel32" (ByVal
nStdHandle As HandleType) As IntPtr

Private Declare Auto Function FillConsoleOutputCharacter Lib
"kernel32" _
(ByVal hConsoleOutput As IntPtr, _
ByVal cCharacter As Char, _
ByVal nLength As Integer, _
ByVal dwWriteCoord As COORD, _
ByRef lpNumberOfCharsWritten As Integer) As Boolean

Private Declare Function FillConsoleOutputAttribute Lib "kernel32" _
(ByVal hConsoleOutput As IntPtr, _
ByVal wAttribute As Short, _
ByVal nLength As Integer, _
ByVal dwWriteCoord As COORD, _
ByRef lpNumberOfAttrsWritten As Integer) As Boolean

Private Declare Function GetConsoleScreenBufferInfo Lib "kernel32" _
(ByVal hconsoleoutput As IntPtr, _
ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Boolean

Private Declare Function SetConsoleCursorPosition Lib "kernel32" _
(ByVal hConsoleOutput As IntPtr, _
ByVal dwCursorPosition As COORD) As Boolean



End Module

HTH
 
So I was right then !. Christ, it seems like an awfull lot of trouble just
to clear the screen. MS should have exposed this functionality within the
console class. This is a very bad oversight in my opinion.

Well done for finding the solution out there though !

Regards - OHM


Tom said:
Please send me exemple
what do you do for cleaning screen in VB for application
console.


Option Strict On
Option Explicit On

Imports System.Runtime.InteropServices

Module Module1

Sub Main()
Console.WriteLine("A line of text....")

Console.ReadLine()

CLS()

Console.WriteLine("A new line of text...")
End Sub

' adapted from the msdn example:
'






http://msdn.microsoft.com/library/d...ry/en-us/dllproc/base/clearing_the_screen.asp
Public Sub CLS() Dim hOutput As IntPtr =
GetStdHandle(HandleType.Output) Dim coordScreen As New COORD(0, 0)
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO Dim dwConSize As Integer Dim
cWritten As Integer

GetConsoleScreenBufferInfo(hOutput, csbi)
dwConSize = csbi.dwSize.X * csbi.dwSize.Y
FillConsoleOutputCharacter( _
hOutput, " "c, dwConSize, coordScreen, cWritten)
FillConsoleOutputAttribute( _
hOutput, csbi.wAttributes, dwConSize, coordScreen, cWritten)
SetConsoleCursorPosition(hOutput, coordScreen)
End Sub

Private Enum HandleType
Input = -10
Output = -11
[Error] = -12
End Enum

<StructLayout(LayoutKind.Sequential)> _
Private Structure COORD
Public X As Short
Public Y As Short

Public Sub New(ByVal X As Short, ByVal Y As Short)
Me.X = X
Me.Y = Y
End Sub
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure SMALL_RECT
Public Left As Short
Public Top As Short
Public Right As Short
Public Bottom As Short
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure CONSOLE_SCREEN_BUFFER_INFO
Public dwSize As COORD
Public dwCursorPosition As COORD
Public wAttributes As Short
Public srWindow As SMALL_RECT
Public dwMaximumWindowSize As COORD
End Structure

Private Declare Function GetStdHandle Lib "kernel32" (ByVal
nStdHandle As HandleType) As IntPtr

Private Declare Auto Function FillConsoleOutputCharacter Lib
"kernel32" _
(ByVal hConsoleOutput As IntPtr, _
ByVal cCharacter As Char, _
ByVal nLength As Integer, _
ByVal dwWriteCoord As COORD, _
ByRef lpNumberOfCharsWritten As Integer) As Boolean

Private Declare Function FillConsoleOutputAttribute Lib
"kernel32" _ (ByVal hConsoleOutput As IntPtr, _
ByVal wAttribute As Short, _
ByVal nLength As Integer, _
ByVal dwWriteCoord As COORD, _
ByRef lpNumberOfAttrsWritten As Integer) As Boolean

Private Declare Function GetConsoleScreenBufferInfo Lib
"kernel32" _ (ByVal hconsoleoutput As IntPtr, _
ByRef lpConsoleScreenBufferInfo As
CONSOLE_SCREEN_BUFFER_INFO) As Boolean

Private Declare Function SetConsoleCursorPosition Lib "kernel32" _
(ByVal hConsoleOutput As IntPtr, _
ByVal dwCursorPosition As COORD) As Boolean



End Module

HTH
 
MS should have exposed this functionality within the
console class. This is a very bad oversight in my opinion.

It appears your laments have been answered. In 1.2 (2.0) of the framework,
the Console class has added functionality for clearing the console, setting
colors, reading a single key, and setting the cursor position to an
arbitrary point on the screen. Much improved.
 
By version 1.2, do you refer to what is codenamed Whidbey, or is there a
framework upgrade available now?


Regards - OHM
 
OHM,
1.2 is Whidbey.

Hope this helps
Jay

One Handed Man said:
By version 1.2, do you refer to what is codenamed Whidbey, or is there a
framework upgrade available now?


Regards - OHM
 
It appears your laments have been answered. In 1.2 (2.0) of the framework,
the Console class has added functionality for clearing the console, setting
colors, reading a single key, and setting the cursor position to an
arbitrary point on the screen. Much improved.

What about multiple screen buffers, scrolling, etc? But, it does sound
much improved, almost usable :)
 
Back
Top