Results 1 to 12 of 12

Thread: Clearing the Console screen

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Location
    Fort Hood, Texas
    Posts
    7

    Question Clearing the Console screen

    Yes I know,

    Who in the world would use console in VB.Net . . . The instructor for the .Net class I'm in.

    I'm using Deitels Visual Basic .Net (How to Program second edition) and it doesn't give any examples or explanation on clearing a console window. I know C++ uses system("cls") to clear the screen, I'm pretty sure .Net has a similar feature.

    My program works fine, it loops like its supposed to do.

    I just wanted to know if there is any way to clear the console window between loops?

    My program flows like this:

    1. initilize varables and set flag to true

    2. begin loop (test if flag is true) (if flag is false skip loop)
    prompt user for data
    get user responce

    calculations done
    assign calculation values to varables

    prompt user if they wish to do another calculation
    get user responce
    if yes loop through the process until user enters no

    **** This is where I want to clear the screen ****
    **** what code if any is needed here? ****

    3. If No then set flag to false and move to the top of the loop
    4. exit program
    Last edited by -]SC[-Dargoth; Sep 5th, 2003 at 01:19 PM.

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    \m/\m/

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Location
    Fort Hood, Texas
    Posts
    7

    Wow that was a fast answer

    Thanks alot PT Exorcist for such a prompt responce, I will look at the link and see what wisdom and insight I can glean from it.

    Again, thanks a ton.

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i never used that class but i think u just have to use the .Clear() method
    \m/\m/

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Location
    Fort Hood, Texas
    Posts
    7
    Do I need to import an additional source ?

    When I type console. I do not get clear() as a selection choice.

    I initially assumed it would be as simple as .clear but it is not listed as choice. Then again this my first project in .Net and it could be me screwin somthin up.

    Any other ideas ?

    Thanks btw

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    if its ur first vb project it might look a bit hard to do it...the thing is, you must create another code file and put in there the class(code) the guy from the other post provided, and from that use it.
    \m/\m/

  7. #7
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    here is an example of how to do it ..if u dont understand something(or just all) say something..

    http://www.vbforums.com/attachment.p...postid=1465983
    \m/\m/

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ok here is how to do it:
    create a new module and then put this in there:
    VB Code:
    1. Strict On
    2. Imports System.Runtime.InteropServices
    3. Imports System.Text
    4.  
    5. Public NotInheritable Class ConsoleEx
    6. #Region " Declarations "
    7.         Private hConsoleIn, hConsoleOut As IntPtr
    8.         Private conInfo As Win32Native.CONSOLE_INFO
    9.         Private cursorInfo As Win32Native.CURSOR_INFO
    10.         Private backColor As Integer
    11.         Private backgroundAttrib As Short
    12.  
    13.  
    14.         Enum InputMode
    15.             LineInput
    16.             EchoInput
    17.         End Enum
    18.  
    19.         Public Enum ConsoleColor
    20.             Black = 0
    21.             Blue = Win32Native.FOREGROUND_BLUE
    22.             Green = Win32Native.FOREGROUND_GREEN
    23.  
    24.             SkyBlue = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN
    25.  
    26.             Red = Win32Native.FOREGROUND_RED
    27.             Purple = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_RED
    28.             Brown = Win32Native.FOREGROUND_GREEN + Win32Native.FOREGROUND_RED
    29.             White = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN + _
    30.                     Win32Native.FOREGROUND_RED
    31.             Gray = Win32Native.FOREGROUND_INTENSIFY
    32.             BlueForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_INTENSIFY
    33.             GreenForte = Win32Native.FOREGROUND_GREEN + Win32Native.FOREGROUND_INTENSIFY
    34.             SkyBlueForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN + _
    35.                            Win32Native.FOREGROUND_INTENSIFY
    36.             RedForte = Win32Native.FOREGROUND_RED + Win32Native.FOREGROUND_INTENSIFY
    37.             PurpleForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_RED + _
    38.             Win32Native.FOREGROUND_INTENSIFY
    39.             Yellow = Win32Native.FOREGROUND_GREEN + Win32Native.FOREGROUND_RED + _
    40.             Win32Native.FOREGROUND_INTENSIFY
    41.             WhiteForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN + _
    42.                          Win32Native.FOREGROUND_RED + Win32Native.FOREGROUND_INTENSIFY
    43.         End Enum
    44.  
    45.         Public Enum CursorType
    46.             Off
    47.             SingleLine
    48.             Block
    49.         End Enum
    50. #End Region
    51.  
    52.         Public Sub New()
    53.             ' Creates a console if there isn't already one loaded.
    54.             Win32Native.AllocConsole()
    55.  
    56.             ' Set the input handle
    57.             hConsoleIn = Win32Native.GetStdHandle(Win32Native.STD_INPUT_HANDLE)
    58.             ' Set the output handle
    59.             hConsoleOut = Win32Native.GetStdHandle(Win32Native.STD_OUTPUT_HANDLE)
    60.  
    61.  
    62.             ' Fill in the console information to conInfo
    63.             conInfo = New Win32Native.CONSOLE_INFO
    64.             updateConsoleInfo()
    65.  
    66.             ' Fill in the cursor information for this console
    67.             cursorInfo = New Win32Native.CURSOR_INFO
    68.             SetCursorType(CursorType.SingleLine)
    69.  
    70.             ' Set the standard background attribute
    71.             backgroundAttrib = CShort((CShort(ConsoleColor.Black) * &H10 + CShort(ConsoleColor.White)))
    72.         End Sub
    73.  
    74.         Protected Overrides Sub Finalize()
    75.             MyBase.Finalize()
    76.             FreeConsole()
    77.         End Sub
    78.  
    79.         Public Property Title() As String
    80.             Get
    81.                 Dim buffer As New StringBuilder(128)
    82.                 Win32Native.GetConsoleTitle(buffer, 128)
    83.                 Return buffer.ToString
    84.             End Get
    85.  
    86.             Set(ByVal Value As String)
    87.                 Win32Native.SetConsoleTitle(Value)
    88.             End Set
    89.         End Property
    90.  
    91.         Public ReadOnly Property Columns() As Integer
    92.             Get
    93.                 Return conInfo.MaxSize.x
    94.             End Get
    95.         End Property
    96.  
    97.         Public ReadOnly Property Rows() As Integer
    98.             Get
    99.                 Return conInfo.MaxSize.y
    100.             End Get
    101.         End Property
    102.  
    103.         ' X position of the cursor (or the carret) on the screen
    104.         Public ReadOnly Property CursorX() As Integer
    105.             Get
    106.                 updateConsoleInfo()
    107.                 Return conInfo.CursorPosition.x
    108.             End Get
    109.         End Property
    110.  
    111.         ' Y position of the cursor (or the carret) on the screen
    112.         Public ReadOnly Property CursorY() As Integer
    113.             Get
    114.                 updateConsoleInfo()
    115.                 Return conInfo.CursorPosition.y
    116.             End Get
    117.         End Property
    118.  
    119.         ' LineInput will let the console read a line at a time. EchoInput lets the console
    120.         ' read each key stroke, as the user types something (You have to use Console.Read ()
    121.         ' instead of Console.ReadLine() when in EchoInput.)
    122.         Public Sub SetMode(ByVal mode As InputMode)
    123.             Dim conMode As Integer
    124.             Win32Native.GetConsoleMode(hConsoleIn, conMode)
    125.  
    126.             If mode = InputMode.EchoInput Then
    127.                 conMode = conMode And Not (Win32Native.ENABLE_LINE_INPUT Or Win32Native.ENABLE_ECHO_INPUT)
    128.             Else
    129.                 conMode = conMode Or (Win32Native.ENABLE_LINE_INPUT Or Win32Native.ENABLE_ECHO_INPUT)
    130.             End If
    131.             Win32Native.SetConsoleMode(hConsoleIn, conMode)
    132.         End Sub
    133.  
    134.         Public Sub Clear()
    135.             Dim written As Integer = 0
    136.             Dim startCoord As New Win32Native.COORD
    137.             startCoord.x = 0 : startCoord.y = 0
    138.  
    139.             Win32Native.FillConsoleOutputCharacter(hConsoleOut, " "c, conInfo.MaxSize.x * conInfo.MaxSize.y, startCoord, written)
    140.             Win32Native.FillConsoleOutputAttribute(hConsoleOut, backgroundAttrib, conInfo.MaxSize.x * conInfo.MaxSize.y, startCoord, written)
    141.  
    142.             MoveCursor(1, 1)
    143.         End Sub
    144.  
    145.         ' Use only if the console is in LineInput mode. If value is true, then the things
    146.         ' that the user types will automatically show up in the console. (In EchoInput mode
    147.         ' nothing shows up automatically)
    148.         Public Sub EchoInput(ByVal value As Boolean)
    149.             Dim Ret As Integer
    150.             Win32Native.GetConsoleMode(hConsoleIn, Ret)
    151.             If value Then
    152.                 Ret = Ret Or (Win32Native.ENABLE_ECHO_INPUT)
    153.             Else
    154.                 Ret = Ret And Not (Win32Native.ENABLE_ECHO_INPUT)
    155.             End If
    156.             Win32Native.SetConsoleMode(hConsoleIn, Ret)
    157.         End Sub
    158.  
    159.         Public Sub SetColor(ByVal foreColor As ConsoleColor, ByVal backColor As ConsoleColor)
    160.             Me.backColor = CInt(backColor)
    161.             SetColor(foreColor)
    162.         End Sub
    163.  
    164.         Public Sub SetColor(ByVal foreColor As ConsoleColor)
    165.             Win32Native.SetConsoleTextAttribute(hConsoleOut, CInt(foreColor) + 16 * backColor)
    166.         End Sub
    167.  
    168.         Public Sub SetClsColor(ByVal backColor As ConsoleColor)
    169.                backgroundAttrib = CShort(backColor) * &H10S
    170.         End Sub
    171.  
    172.         Public Sub MoveCursor(ByVal x As Integer, ByVal y As Integer)
    173.             conInfo.CursorPosition.x = CShort(x - 1)
    174.             conInfo.CursorPosition.y = CShort(y - 1)
    175.             If cursorInfo.Visible Then
    176.                 Dim coord As Integer = conInfo.CursorPosition.x + conInfo.CursorPosition.y * &H10000
    177.                 Win32Native.SetConsoleCursorPosition(hConsoleOut, coord)
    178.             End If
    179.         End Sub
    180.  
    181.  
    182.         ' Change the cursor's shape and visibility state.
    183.         Public Sub SetCursorType(ByVal newType As CursorType)
    184.             '  Change the cursor type.
    185.             Select Case newType
    186.                 Case CursorType.Block
    187.                     cursorInfo.Size = 100
    188.                     cursorInfo.Visible = True
    189.  
    190.                 Case CursorType.SingleLine
    191.                     cursorInfo.Size = 10
    192.                     cursorInfo.Visible = True
    193.  
    194.                 Case CursorType.Off
    195.                     cursorInfo.Size = 100
    196.                     cursorInfo.Visible = False
    197.             End Select
    198.  
    199.             Win32Native.SetConsoleCursorInfo(hConsoleOut, cursorInfo)
    200.  
    201.             ' Move the cursor to its correct position.
    202.             MoveCursor(conInfo.CursorPosition.x, conInfo.CursorPosition.y)
    203.         End Sub
    204.  
    205.         ' Forces to close the console. Don't call if not necessary.
    206.         Public Sub FreeConsole()
    207.             ' I just added these myself. I don't if I really have to do this, but I thought
    208.             ' they are necessary. That's why it's in a Try-Catch block, because I don't
    209.             ' know if it may cause an error or not. But it shouldn't.
    210.             Try
    211.                 Win32Native.FreeConsole()
    212.                 Win32Native.CloseHandle(hConsoleIn)
    213.                 Win32Native.CloseHandle(hConsoleOut)
    214.                 Win32Native.FreeConsole()
    215.             Catch
    216.             End Try
    217.         End Sub
    218.  
    219.  
    220.         Private Sub updateConsoleInfo()
    221.             ' Fill in the console information to conInfo
    222.             Win32Native.GetConsoleScreenBufferInfo(hConsoleOut, conInfo)
    223.         End Sub
    224.     End Class

    then in ur main function put:
    VB Code:
    1. Dim ConEx as new ConsoleEx
    2. Console.Writeline("hiiiii")
    3. Console.ReadLine()
    4. ConEx.Clear()
    thu i didnt make it i think it will work
    \m/\m/

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Location
    Fort Hood, Texas
    Posts
    7
    COOLNESS PT Exorcist,

    You da man . . . er (or woman if it fits)

    Its guys like you that truely make these forums a real pleasure. I really do appreciate your help and you sharing your experience at .net issues.
    I spent all yesterday trying to search the web and the MSDN sit for this console problem and nothing.

    I post here and within 10 mins I get a responce (and a dam good one I might add)

    again I really appreciate your help and I will sit down and play with this code once I get home.

    Dargoth

  10. #10
    New Member
    Join Date
    Sep 2003
    Location
    UK
    Posts
    8

    Wink Clear Console Window (Resolved)


  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Location
    Fort Hood, Texas
    Posts
    7
    Man o Man,

    I tell ya you guys(girls) are great. Thank you for the Link crouchie1998, I haven't tried it yet but it looks promising.

    again thanks a ton Crouchie1998

  12. #12
    New Member
    Join Date
    Sep 2003
    Location
    UK
    Posts
    8

    Thumbs up

    You're Welcome. Glad to help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width