
正文
vb.net退出命令,可以通过菜单中的命令退出vb
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
VB.NET命令窗口
Vb不是人机对话窗口,而是编程工具,可以编写代码进行调试执行或者生成可执行文件。
用Visual Studio .Net开发"Hello World!"程序:
我们知道"Hello World"一般是命令行程序,而这种程序在Visual Studio .Net中被称为"控制台程序"。下面是Visual Studio .Net编写控制台"Hello World!"程序的具体步骤:
(1)、启动Visual Studio .Net。
(2)、选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框。
(3)、将【项目类型】设置为【Visual Basic项目】。
(4)、将【模板】设置为【控制台应用程序】。
(5)、在【名称】文本框中输入【Hello World】。
(6)、在【位置】的文本框中输入【E:VS.NET项目】,然后单击【确定】按钮,则Visual Studio .Net会按照上面设定的参数创建一个控制台应用程序项目,具体如图01所示。
图01:"Hello World"控制台程序的【新建项目】界面
(7)、在【解决方案资源管理器】窗口中,双击Module1.vb文件,进入Module1.vb文件的编辑界面。
(8)、Visual Studio .Net已经为产生Main()函数。在系统创建的Main()函数中加入下面一行代码就可以了:
Console.WriteLine ("Hello World!")
(9)、选择【文件】|【保存Module1.vb】菜单或者按快捷键Ctrl+S,保存所做的修改。
(10)、此时单击快捷键Ctrl+F5运行程序就可以得到图02所示运行界面:
相关问答
Q1: 详细阐述 vb.net 中main
每个 Visual Basic 应用程序均必须包含一个称为VB.NET Main过程。该过程为应用程序的起始点并为应用程序提供总体控制。.NET Framework 在已加载应用程序并准备将控制传递给它时,将调用 Main 过程。除非您要创建 Windows 窗体应用程序,否则就必须为自运行的应用程序编写 Main 过程。
Main 中包含首先运行的代码。在 Main 中,可以确定在程序启动时首先加载的窗体,确定系统上是否已在运行您的应用程序副本,为应用程序建立一组变量,或者打开应用程序需要的数据库。
VB.NET Main过程的要求
独立运行的文件(扩展名通常为 .exe)必须包含 Main 过程。库(例如,扩展名为 .dll)不独立运行,因而不需要 Main 过程。可以创建的不同类型的项目的要求如下:
控制台应用程序可以独立运行,而且您必须提供至少一个 Main 过程。
Windows 窗体应用程序可以独立运行。但是,Visual Basic 编译器会在此类应用程序中自动生成一个 Main 过程,因而您不需要编写此过程。
类库不需要 Main 过程。这些类库包括 Windows 控件库和 Web 控件库。作为类库部署 Web 应用程序。
声明VB.NET Main过程
有四种方法可以声明 Main 过程。它可以使用参数或不使用参数,可以返回值或不返回值。
注意
如果在类中声明 Main 过程,则必须使用 Shared 关键字。在模块中,Main 不必是 Shared。
最简单的方法是声明一个不使用参数或不返回值的 Sub 过程。
Module mainModule
Sub Main()
MsgBox("The Main procedure
is starting the application.")
' Insert call to appropriate
starting place in your code.
MsgBox("The application
is terminating.")
End Sub
End ModuleMain
还可以返回一个 Integer 值,操作系统将其作为程序的退出代码。其他程序可以通过检查 Windows ERRORLEVEL 值来测试该代码。若要返回退出代码,必须将VB.NET Main过程声明为 Function 过程而不是 Sub 过程。
Module mainModule
Function Main() As Integer
MsgBox("The Main procedure
is starting the application.")
Dim returnValue As Integer = 0
' Insert call to appropriate
starting place in your code.
' On return, assign appropriate
value to returnValue.
' 0 usually means successful
completion.
MsgBox("The application is
terminating with error level " _
CStr(returnValue) ".")
Return returnValue
End Function
End ModuleMain
还可以采用一个 String 数组作为参数。数组中的每个字符串均包含一个用于调用程序的命令行参数。您可以根据它们的值采取不同的操作。
Module mainModule
Function Main(ByVal cmdArgs()
As String) As Integer
MsgBox("The Main procedure is
starting the application.")
Dim returnValue As Integer = 0
' See if there are any arguments.
If cmdArgs.Length 0 Then
For argNum As Integer = 0 To
UBound(cmdArgs, 1)
' Insert code to examine cmdArgs
(argNum) and take
' appropriate action based on its value.
Next argNum
End If
' Insert call to appropriate starting
place in your code.
' On return, assign appropriate
value to returnValue.
' 0 usually means successful completion.
MsgBox("The application is
terminating with error level " _
CStr(returnValue) ".")
Return returnValue
End Function
End Module
可以声明VB.NET Main过程来检查命令行参数而不返回退出代码,如下所示。
Module mainModule
Sub Main(ByVal cmdArgs() As String)
MsgBox("The Main procedure is
starting the application.")
Dim returnValue As Integer = 0
' See if there are any arguments.
If cmdArgs.Length 0 Then
For argNum As Integer = 0 To
UBound(cmdArgs, 1)
' Insert code to examine cmdArgs
(argNum) and take
' appropriate action based on its value.
Next argNum
End If
' Insert call to appropriate
starting place in your code.
MsgBox("The application is
terminating."
End Sub
End Module
Q2: vb命令按钮退出怎么设置?
在退出按钮的Click事件中用Unload
Me就可以关闭窗体。
Private
Sub
Command1_Click()
Unload
Me
End
Sub
答案补充
代码就是我回答那些了,双击按钮把代码输入进去就行了。对了,你的是VB6还是VB.Net啊?VB6就是用Unload
Me,如果是VB.Net应该用Me.Close()。
这个是VB.Net的代码:
Private
Sub
Button1_Click(ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button2.Click
Me.Close()
End
Sub




