vb.net 如何在VB中打开控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21055166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:25:27  来源:igfitidea点击:

How to Open console in VB

vb.netvisual-studio-2010console

提问by user2874682

I currently have a console applicationby using the setting illustrated in the image bellow. However Now I wish to open multiple forms with the consoleso I'm wondering if I can somehow open multiple forms or open the consolewithin a Windows Forms Application

我目前有一个console application使用下图所示的设置。但是现在我想用 来打开多个表单,console所以我想知道我是否可以以某种方式打开多个表单或打开console一个Windows Forms Application

Here is the Project's settings

这是项目的设置

回答by tinstaafl

@tinstaafl can you share this extra programming or a link to a solution. Thanks

@tinstaafl 你能分享这个额外的程序或解决方案的链接吗?谢谢

Here's a couple of links:

这是几个链接:

Console and WinForm together for easy debugging

控制台和 WinForm 一起,便于调试

Console Enhancements

控制台增强功能

Here's a conversion of the first one. You'll need a form with a checkbox name "CheckBox1":

这是第一个的转换。您需要一个复选框名称为“CheckBox1”的表单:

Imports System.Runtime.InteropServices

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            Win32.AllocConsole()
            Console.WriteLine("Done!")
        Else
            Win32.FreeConsole()
        End If
    End Sub
End Class
Public Class Win32
    <DllImport("kernel32.dll")> Public Shared Function AllocConsole() As Boolean

    End Function
    <DllImport("kernel32.dll")> Public Shared Function FreeConsole() As Boolean

    End Function

End Class

Everytime you click the checkbox you show or hide the console. You can write to and read from the same as any console app.

每次单击复选框时,都会显示或隐藏控制台。您可以像任何控制台应用程序一样写入和读取。

回答by JaredPar

Forms and Console applications are very different. So much so that generally speaking a process either needs to be a form or console application. Forms applications are implemented with a message pump and console applications are command line drive. It is possible to a degree to run a form within a console, and vice versa, but generally not recommended. If you truly need both I would highly encourage you to use 2 processes.

表单和控制台应用程序非常不同。如此之多以至于一般来说,流程要么需要是表单,要么是控制台应用程序。表单应用程序是用消息泵实现的,控制台应用程序是命令行驱动。在某种程度上可以在控制台中运行表单,反之亦然,但通常不推荐。如果您真的需要两者,我强烈建议您使用 2 个流程。

If you could elaborate a bit more on your use case we may be better able to help you out.

如果您能详细说明您的用例,我们可能会更好地为您提供帮助。

回答by Carl

So this is very cool. In the designer just add a checkbox using the Toolbox common controls.

所以这很酷。在设计器中,只需使用工具箱常用控件添加一个复选框即可。

Then double click on the new "CheckBox1" and that will automatically insert this sub routine:

然后双击新的“CheckBox1”,它会自动插入这个子程序:

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

End Sub

Then all you have to do is add this code:

然后你所要做的就是添加以下代码:

    If CheckBox1.Checked Then
        Win32.AllocConsole()
        Console.WriteLine("Done!")
    Else
        Win32.FreeConsole()
    End If

When you run your windows form program and check the box it will automatically open the window and KEEP it open until you uncheck the box.

当您运行 Windows 窗体程序并选中该框时,它将自动打开该窗口并使其保持打开状态,直到您取消选中该框为止。

Add this class to the bottom of your program:

将此类添加到程序底部:

Public Class Win32 

    <DllImport("kernel32.dll")> Public Shared Function AllocConsole() As Boolean

    End Function
    <DllImport("kernel32.dll")> Public Shared Function FreeConsole() As Boolean

    End Function

End Class

And be sure to add the Imports statement at the top

并确保在顶部添加 Imports 语句

Imports System.Runtime.InteropServices

回答by Carl

If you want to open a console window to interact with and when you close the console, that action won't terminate your windows program then you can add these two lines of code:

如果您想打开一个控制台窗口进行交互,当您关闭控制台时,该操作不会终止您的 Windows 程序,那么您可以添加以下两行代码:

Dim myProcess As Process

Dim myProcess 作为进程

myProcess = Process.Start("cmd.exe")

myProcess = Process.Start("cmd.exe")