Console.Clear()

  • Thread starter Thread starter Just Me
  • Start date Start date
J

Just Me

I am writing a small windows applicationa and I am writing console.writeline
data to the console window. But i wanted to clear the area before writing to
the window.

I found the console.clear method, but when I run it it fails with an
uinhandled exception and the exception is that it does not have the handle
to the window.

IM assuming, I need to determine the console windo handle, but Im not sure,
has anyone used this, can point me in the right direction.


Thanks a million
 
I am writing a small windows applicationa and I am writing console.writeline
data to the console window. But i wanted to clear the area before writing to
the window.

I found the console.clear method, but when I run it it fails with an
uinhandled exception and the exception is that it does not have the handle
to the window.

IM assuming, I need to determine the console windo handle, but Im not sure,
has anyone used this, can point me in the right direction.

Thanks a million

I'm not sure I follow you - I can use Console.Clear with no problems:

////////////////////////
Module Module1

Sub Main()
For i As Integer = 0 To 5
Console.WriteLine("hello world {0}", i.ToString())
Next i

System.Threading.Thread.Sleep(1000)

Console.Clear()

Console.WriteLine("Console Cleared")

Console.Read()
End Sub

End Module
////////////////////////

The above runs with no problems for me - is it throwing an exception
for you? If not please post a small but complete code sample that can
reproduce the error for me.

Thanks,

Seth Rowe
 
Yes, but you are using a console application. I am viewing the output window
in windows forms application.
 
Yes, but you are using a console application. I am viewing the output window
in windows forms application.

Alright, would you mind posting some code that I can use to replicate
the problem?

Thanks,

Seth Rowe
 
sure. its a vb.net windows applicaiton with one form.

'//In a button handler

console.clear()
console.writeline("Hello World")
'// Thats it.

Cheers
 
sure. its a vb.net windows applicaiton with one form.

'//In a button handler

console.clear()
console.writeline("Hello World")
'// Thats it.

Cheers

Okay, now I'm getting worried. Did you run the application without the
Console.Clear() call? Unless I've really forgotten something Window's
based applications ignore the calls to the Console class, as it
doesn't exist.

Excerpt from the MSDN documentation (http://msdn2.microsoft.com/en-us/
library/system.console(VS.71).aspx):

<quote>
The Console class provides basic support for applications that read
characters from, and write characters to, the console. If the console
does not exist, as in a Windows-based application, writes to the
console are not displayed and no exception is raised.
</quote>

If you really need to have both a console window and standard forms
you need to create a console project. Then reference the appropriate
dll's (such as System.Windows.Forms) and then create the form classes
you need.

The following code sample requires a new console project and
references to System.Windows.Forms and System.Drawing to work.

////////////////
Imports System.Windows.Forms
Imports System.Drawing

Module Module1

Sub Main()
Application.EnableVisualStyles()

Console.WriteLine("Showing form...")
Console.WriteLine("Be sure to look behind me, the form is
sometimes hidden behind me")
System.Threading.Thread.Sleep(1000)

Dim f As New Form()
f.Text = "My Custom Form"

Dim b As New Button()
AddHandler b.Click, AddressOf button_Click
b.Width = 100
b.Location = New Point(100, 100)
b.Text = "Click Me"
f.Controls.Add(b)

f.ShowDialog()

Console.WriteLine("You just closed the form")
Console.WriteLine("Goodbye")

System.Threading.Thread.Sleep(1000)
End Sub

Private Sub button_Click(ByVal sender As Object, ByVal e As
EventArgs)
Console.Clear()
Console.WriteLine("Hello World from the Windows Form!")
Console.WriteLine("The current time is: {0}",
DateTime.Now.ToString("h:m:s tt"))
End Sub

End Module
////////////////

Hope That helps!

Thanks,

Seth Rowe
 
I allways use console as a diagnostic tool. Its found often in the examples.
Its just that It would be nice to clear the console window, this method was
never available in 1.x of the framework. Dont worry about it, if you dont
know, its not the end of the earth.

Thanks anyway
 
I allways use console as a diagnostic tool. Its found often in the examples.
Its just that It would be nice to clear the console window, this method was
never available in 1.x of the framework. Dont worry about it, if you dont
know, its not the end of the earth.

What?

One of us is very confused. The Console does not exist in windows
forms, therefore you cannot write to it and you can't clear it. Sure
you can call "Console.WriteLine("Hello World")" but that does nothing
since the console doesn't exist. If you want to have the console you
have to create it like I showed you in my previous code sample.

My question is how do you use the console as a diagnostic tool for
Windows forms when it doesn't exist by default? When you call
Console.WriteLine() where is the text showing up? Are you sure you
aren't talking about Visual Studio's Output window and not the
Console?

Thanks,

Seth Rowe
 
I allways use console as a diagnostic tool. Its found often in the
examples. Its just that It would be nice to clear the console window,
this method was never available in 1.x of the framework. Dont worry
about it, if you dont know, its not the end of the earth.

Thanks anyway

Forgive the interruption..

Can I just clarrify the term "Console"?

To me this means the black "dos-like" window that results from an application
of type console application right. There is the (possibly remote) posibility
that you in fact mean the "output" window. (Normally written to using Debug.writeline)

I can't say I have ever used the console window for debug information from
a windows application. Indeed I had not thought it would be possible.

I instead use Log4Net. It looks a bit daunting at first but allows for turning
on and off debug information whilst your application is still running and
is very granular.

Perhaps you will find this useful.

Again sorry for the interruption. I have been observing this thread and was
very curious to see what the answer would be.
 
No No,

The output window receives the console.writeline type output, this is what
Im trying to clear
 
No No,
The output window receives the console.writeline type output, this is
what Im trying to clear

Ok I'm not sure if you're using a non english version of VS where the *Output*
Panel (White and dockable with VS) or if you're managed to alias/wrap the
debug.output in some console class but what you're saying *appears* to be
completely wrong.

The normal/Default situation is:
Console = Black and independent , Output = White and dockable.

You can clear the console but I doubt you can clear the output window (at
least via a debug.Clear command). Although in truth I have not looked for
this.
 
where the *Output* Panel (White and dockable with VS)

Sorry I meant to add "might be named 'console'" to the end of this.
 
Im not sure why your having a problem. Here is the code from my button. The
compiler messages appear in the same window as the output from this command.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Console.WriteLine("hello")

End Sub
 
Im not sure why your having a problem. Here is the code from my button. The
compiler messages appear in the same window as the output from this command.

Yes, but what they appear in is not the Console, but the Output
window. Again, you are talking about the Output window and NOT the
console window - please quit referring to it as the console window.

Now that we now what you are talking about, here is your answer, and
no it's not what you're hoping for:

The only way to clear the Output window is to right-click it and
select Clear All, it cannot be done via standard VB.Net code.

Thanks,

Seth Rowe
 
Yes, I never said it was the console window. I made reffernence several
times to the output window. Dont you think its curious though that you can
use the console.writeline member function to output to the output window,
but cant use the console.clear member to remove the crap on the output
window.

As for your answer, you do seem to be a little stressed over it, you have no
need to be, the question was valid and only required an answer from someone
who knew it.

There is actually a way to do this, it uses the environment namespace, and
an assembly wrapped COM object. However, its probably not worth the bother
tbh.


Thanks Anyway
 
Im not sure why your having a problem. Here is the code from my
button. The compiler messages appear in the same window as the output
from this command.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Console.WriteLine("hello")
End Sub

ok I believe the mis-understanding comes from the fact that I (and perhaps
Seth) was unaware that Console.WriteLine would write to the output window.

Hence our (or at least my) assumption that you were confused in some fashion.


Sorry about that.

To my mind this was strange. I came from VB6 where I had access to a print
statement which I used for debuging purposes (like how you are using Console.Writeline).
When I moved to VB.Net one of my first questions was how to do the equivilent
.. The answer I recieved was to use the 'debug' class. Fair enough.

I later discovered the Console class... but only as a part of a console application.

I would have thought that the output of console being reflected in the output
window (whilst a nice enough side effect) will not serve you as well in the
future. For example, if you sudddenly need to use a businessobject from within
a console application.

I would recommend switching to using either the 'trace' class , the 'debug'
class or Log4Net

----------------------------------------------
FYI:
Calls to the methods on Trace will (by default) make it into and exe/dll
compiled in release mode.
Calls to the methods on Debug will not (by default) make it into and exe/dll
compiled in release mode.
Calls to the methods in Log4Net will make it into a release mode exe but
their execution can be controlled by a config file.
----------------------------------------------

Sorry we couldn't help provide a solution for your request. ie the clearing
of the output window. I guess you might be ablke to send a message to Studio
in some other way which might result in this behavior, but I'm afraid I have
no information on how you might achieve that.
 
Yes, I never said it was the console window.

Not to be a prick but here are some of your quotes in which you refer
to the Console window and not the Output window:

"I am writing console.writeline data to the console window."

"I need to determine the console windo handle"

"I allways use console as a diagnostic tool" (though now reading this
it seems you could have meant the Console.WriteLine commands and not
the Console)

"Its just that It would be nice to clear the console window,"
I made reffernence several times to the output window.

Before my post, I only see one time you refer to the window as the
Output window. I wish I would have seen it sooner so we could have
possibly avoided this confusion:

"I am viewing the output window in windows forms application."

As you see you referred to the window in question as the Console 3 or
4 times and only as the Output window once. It is very difficult to
help you when you keep using incorrect terms for two similar, but very
distinct, features of the framework.
As for your answer, you do seem to be a little stressed over it, you have no
need to be, the question was valid and only required an answer from someone
who knew it.

Don't worry I stress over very little, this thread was a lot of work
to give just a "nope, sorry" answer.
There is actually a way to do this, it uses the environment namespace, and
an assembly wrapped COM object. However, its probably not worth the bother
tbh.

Yes, but I believe all this code would compile and ship with the
released application, adding unneeded and possibly unstable code to a
project. You do know you can select what gets outputted to the Output
Window right? It may be easier to remove things such as the
Compilation messages than to write a hack that clears the window.

Thanks,

Seth Rowe
 
Your very first post said:

I am writing a small windows applicationa and I am writing
console.writeline data to the console window.

After that you sometimes said "output window" and sometimes "console
window". That's why we got confused.
 
Ok dont you ever use terms interchangably sometimes. Im human, get over it
and stop trying to score points.
 
Ok dont you ever use terms interchangably sometimes.

Yes, and in technical discussions it is a very big deal - as you can
see it took a thread that should have been 2 posts long and is turning
it into one that is 20+ posts in length.
Im human, get over it

You said you never referred to the Output window as the Console. We
showed you where you did. Don't get offended, whenever you say you
didn't type something on Usenet expect to be proven wrong - it's how
things are. We are all human and do/will make mistakes, but saying you
didn't will only get you proven wrong. It's best to just accept the
rebuke and apologize if needed.
and stop trying to score points.

We get points?

Thanks,

Seth Rowe
 
Back
Top