简体   繁体   中英

Console.Write Not Working In Win Forms App

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?

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

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.

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)

尝试使用“控制台应用程序”模板创建一个新项目。

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.

您必须创建一个命令行/控制台应用程序而不是Windows窗体应用程序才能使用控制台。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM