views:

344

answers:

5

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 doing wrong?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Debug.Write("Foo")
    Debug.Flush()
    Console.WriteLine("foo")
    Console.Beep(800, 100) 'confirm this function is called'
    Me.Close()
End Sub

EDIT: Can a program have a form and a console?

EDIT2: Ho's answer works. However, the output appears on the next command-line prompt. Can a Winforms application tell the command-line to wait until it's finished instead of immediately returning?

+1  A: 

You have to create a command-line/console app rather than a Windows form application to use the console.

Tom Cabanski
+2  A: 

Try creating a new project using the "Console Application" template instead.

Simeon
+3  A: 

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)
Dan Tao
+3  A: 

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 :)

Mr Roys
+1  A: 

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);

ho1
VB's version of this works. However, the output appears after the next command-line prompt. I'm now trying to figure out how to prevent the command-line from immediately returning.
Steven
You can do "START yourapp.exe /WAIT" to make the commandline wait, but I don't think that'll give you exactly what you want.
ho1
Actually, that would work just fine. Thanks!
Steven