Question How to show console in a Winforms app

kanaida

Member
Joined
Oct 6, 2009
Messages
15
Programming Experience
3-5
I've made console apps that show what they are doing, open forms etc...

now I don't always want to show the debug console.

I want to make a Winforms app, that starts as a form. and in some menu open that same console that i see in a console project.

how can i do that?
 
To open a Windows command prompt you simply run the cmd.exe program, which you can do by calling Process.Start. Note that it's going to be a completely separate process to your WinForms app though.
 
that's not what i meant

I need the console for my application as part of it. not cmd.exe

I had the code for this a while back, but it was lost in time.

I mean how does the console project do it?
 
reason i need it

basically, the point of having it is because all over the place I can safely call Console.Writeline()
this is of amazing use when you do it consistently in every, function, sub etc... works across multiple threads, no problem, it serves as a realtime log of what the application is doing.

usually i write this at the beginning of every sub()
Console.writeline(now.tostring & " MySub(ParamValue1,ParamValue2)

it's also a great help to identify wich parts of the program take a long time, especially queries since i don't have the luxury of using sql :) since all the data is in foxpro 2.6, being shared from a linux box.

it works so well that every time a bug comes up, I know exactly what line caused it in my head without looking at the code most of the time no matter how big my apps grow.
 
You should look into tracing. The Output window in VS simply displays the output of several TraceListeners, including one that listens to Console calls. What you need to do is set up a TraceListener and pipe the output to a TextBox if you want to see it in your app, or else to a file or whatever. You don't have to use Console either, but rather the Trace class.
 
any idea if that class supports color

part of my logging is using a variety of colors to clearly see warnings, errors, data loading and misc messages. does that class you talk about also keep those things?

if I remember correctly, there was something along the lines of opening a console and sending the trace as the input for it somehow...

I'll do a few experiments with that class though, the idea here is to write as little code as possible. I'm trying to avoid re-writing a console clone myself. The other thing is that even if the guy is locked up doing some work, the console does not, so a textbox wont cut it in this particular case.

right now my only work around is to make a dll with all my forms and logic, then creating a console app, referencing the dll and creating an instance of form1 using showdialog() so that the console stays open and shows messages.
 
if you've ever used the PS2 Emulator from pcsx2.net you'll see exactly what i'm talking about. they got the console going doing the same as i do, but they can show or hide it at any time.
 
Interesting to see that the console app can be added. I think it may be easier to fake a console type log window. Add a docked panel to the bottom of your screen and fill it with a multi-line textbox or richtextbox control. You can even set the colors to match the console app if you want. I have a similar type log window in my app that uses a listview control and a simple sub for passing data to it.
 
Below is a sample showing how a console window can be opened and closed after user clicks a form application button:


VB.NET:
Public Class Form1

	Declare Function AllocConsole Lib "kernel32" () As Integer
	Declare Function FreeConsole Lib "kernel32" () As Integer

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim mydata As String
		If Convert.ToBoolean(AllocConsole()) Then
			Console.WriteLine("THIS IS FIRST LINE")
			Console.WriteLine("THIS IS SECOND LINE")
			Console.WriteLine("THIS IS THIRD LINE")
			Console.WriteLine("THIS IS FOURTH LINE")
			Console.WriteLine("THIS IS FIFTH LINE")
			Console.WriteLine()
			Console.Write("Now enter some data:  ")
			mydata = Console.ReadLine
			Console.WriteLine("You wrote:  " & mydata)
			Console.WriteLine()
			Console.WriteLine("Press any key to Clear")
			Console.ReadKey()
			Console.Clear()
			Console.WriteLine("THE CONSOLE WAS CLEARED")
			Console.WriteLine("Press any key to close console")
			Console.ReadKey()
			FreeConsole()

			'System.Environment.Exit(1)		'ends entire application
		End If
	End Sub

End Class
 
Interesting to see that the console app can be added. I think it may be easier to fake a console type log window. Add a docked panel to the bottom of your screen and fill it with a multi-line textbox or richtextbox control. You can even set the colors to match the console app if you want. I have a similar type log window in my app that uses a listview control and a simple sub for passing data to it.

yes i know, but that approach is flawed since display is tied to the gui thread, and wont update independently of it.
 
Below is a sample showing how a console window can be opened and closed after user clicks a form application button:
Very interesting. There are just so many funky things you can do with the Windows API.
 
Back
Top