I created a VB.NET Windows Forms Application in Visual Studio 2008. When I run my program from the command-line, I get no output (only the next prompt).
What am I d
You can run your app from the console using myApp.exe|MORE
This way the console will show Console.WriteLine()
calls coming from the app and will wait until the app exits.
Please excuse my bad english.
Try creating a new project using the "Console Application" template instead.
Tested similar code with a C# .NET Windows Form Application. Outputs and beeps nicely within Visual Studio but only beeps when run at the command line.
If I change the Output Type to Console Application under the Application tab for the Project properties, I get to use both the form and console :)
The solution is:
Declare Function AttachConsole Lib "kernel32.dll" (ByVal dwProcessId As Int32) As Boolean
Declare Function FreeConsole Lib "kernel32.dll" () As Boolean
[code.....]
when needed the console output you just call AttachConsole(-1) but remember to do a FreeConsole() at the end of your program.
the only problem with this solution is that you will read something like this:
C:>yourapp.exe
C:>Hello World!
That's because a forms application runs as a child of the command prompt so the prompt returns immediately after you type the app name..
In a console application the command prompt returns after the program exits.
I am still trying to find a way to have the same behaviour (sync run) as in a console application.
The others are correct in saying that you need to run your app as a console app. To your question of whether you can have both a console and a GUI: yes. Simply add a reference to System.Windows.Forms
to your project, and for the app's Main
method, include this code:
' here instantiate whatever form you want to be your main form '
Dim f As New Form1
' this will start the GUI loop, which will not progress past '
' this point until your form is closed '
System.Windows.Forms.Application.Run(f)
Or if you already have a WinForms application you can attach a Console to it using the AttachConsole API.
using System.Runtime.InteropServices;
...
[DllImport("kernel32.dll")] static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
...
AttachConsole(ATTACH_PARENT_PROCESS);
(Formatted as code)