vb.NET使用vb类而不是窗体启动窗体,但窗体一启动即关闭

vb.NET使用vb类而不是窗体启动窗体,但窗体一启动即关闭,vb.net,visual-studio,app-startup,Vb.net,Visual Studio,App Startup,我想在VB中使用一个对象来启动我的窗体,但是当我使用一个对象时,窗体会打开,但随后会关闭。我正在使用一个vb类作为对象来启动项目属性中的应用程序。我遇到的问题是,一旦它显示出表单,它就会全部关闭。没有什么是开着的。我怎样才能解决这个问题?以下是我的对象和表单代码: AppStarter.vb: Public Class AppStarter Shared Sub Main() If System.IO.File.Exists("C:\Mingw\bin\g++.exe")

我想在VB中使用一个对象来启动我的窗体,但是当我使用一个对象时,窗体会打开,但随后会关闭。我正在使用一个vb类作为对象来启动项目属性中的应用程序。我遇到的问题是,一旦它显示出表单,它就会全部关闭。没有什么是开着的。我怎样才能解决这个问题?以下是我的对象和表单代码:

AppStarter.vb:

Public Class AppStarter
    Shared Sub Main()
        If System.IO.File.Exists("C:\Mingw\bin\g++.exe") Then
            Form1.Show()
        Else
            ErrorPage.Show()
            MsgBox("Test")
        End If
    End Sub
End Class
表格1:

Public Class Form1
    Dim fd As OpenFileDialog = New OpenFileDialog()
    Dim fld As FolderBrowserDialog = New FolderBrowserDialog()
    Dim strFileName As String
    Dim strCompiledFileName As String
    Dim strFileDestination As String
    Dim strFilePath As String
    Dim test As Boolean

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

    End Sub

    Private Sub cmdCompile_Click(sender As Object, e As EventArgs) Handles cmdCompile.Click
        If txtCompiledFileName.Text <> "" And strFilePath <> "" And strFileDestination <> "" Then
            strCompiledFileName = txtCompiledFileName.Text
            Try
                Process.Start("cmd", "/c cd " + strFileDestination + " & g++ " + strFilePath + " -o " + strCompiledFileName)
                MsgBox("Compiled Successfully!")
                ClearVar()
            Catch ex As Exception
                MsgBox(ex)
                ClearVar()
            End Try
        Else
            MsgBox("Missing compiled file name or file path.", MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub cmdChooseFile_Click(sender As Object, e As EventArgs) Handles cmdChooseFile.Click
        fd.Title = "Open File Dialog"
        fd.InitialDirectory = "C:\"
        fd.Filter = "C/C++ Files |*.c; *.cpp"
        fd.FilterIndex = 1
        fd.RestoreDirectory = True
        fd.DereferenceLinks = True

        If fd.ShowDialog() = DialogResult.OK Then
            strFileName = System.IO.Path.GetFileName(fd.FileName)
            strFilePath = fd.FileName
        End If

        lblChosenFile.Text = strFileName
    End Sub

    Private Sub cmdFileDestination_Click(sender As Object, e As EventArgs) Handles cmdFileDestination.Click
        fld.Description = "Select a folder to extract to:"
        fld.ShowNewFolderButton = True
        fld.SelectedPath = strFileDestination
        fld.RootFolder = System.Environment.SpecialFolder.MyComputer

        If fld.ShowDialog() = DialogResult.OK Then
            strFileDestination = fld.SelectedPath
            lblChosenFolder.Text = strFileDestination
        End If
    End Sub

    Public Sub ClearVar()
        txtCompiledFileName.Text = ""
        lblChosenFile.Text = "No chosen file"
        lblChosenFolder.Text = ""
    End Sub
End Class

如果要编写自己的
Main
方法并显示自己的启动表单,则需要调用
应用程序。运行
并将表单作为参数传递。您正在调用的
Show
方法立即返回,因此您的
Main
方法完成,应用程序退出

下面是C#WinForms应用程序的启动方式:

静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
你应该做基本相同的事情:

模块程序
副标题()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(Form1)
端接头
端模块
如果需要将该
应用程序放入
块中。在
If
块中运行
调用,就这样吧

也就是说,这样做可能没有意义。您需要的功能已经内置到VB应用程序框架中。只需按正常方式创建一个VB WinForms应用程序项目,并将启动窗体保持为正常状态。然后,您可以从项目属性打开应用程序事件代码文件,并处理
启动
事件。在该事件处理程序中,如果将
e.Cancel
设置为
True
,则应用程序将退出,而不会创建启动表单。这意味着您可以这样做:

Imports System.IO
导入Microsoft.VisualBasic.ApplicationServices
名称空间我的
'以下事件可用于MyApplication:
'启动:在创建启动窗体之前,在应用程序启动时引发。
'关闭:在关闭所有申请表后引发。如果应用程序异常终止,则不会引发此事件。
'UnhandledException:如果应用程序遇到未处理的异常,则引发。
'StartupNextInstance:在启动单实例应用程序且该应用程序已处于活动状态时引发。
'NetworkAvailabilityChanged:在连接或断开网络连接时引发。
部分友元类MyApplication
私有子MyApplication_Startup(发送方作为对象,e作为StartupEventArgs)处理我。Startup
如果不存在File.Exists(“C:\Mingw\bin\g++.exe”),则
'显示错误页,并确保调用ShowDialog而不是Show。
'退出而不创建启动窗体。
e、 取消=真
如果结束
端接头
末级
结束命名空间
如果文件确实存在,则应用程序将正常启动,并显示启动窗体

Public Class ErrorPage
    Dim webAddress As String = "https://osdn.net/projects/mingw/releases/"

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

    End Sub

    Private Sub cmdYes_Click(sender As Object, e As EventArgs) Handles cmdYes.Click
        Process.Start(webAddress)
    End Sub

    Private Sub cmdNo_Click(sender As Object, e As EventArgs) Handles cmdNo.Click
        Form1.Close()
        Close()
    End Sub


    Private Const CP_NOCLOSE_BUTTON As Integer = &H200
    Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim myCp As CreateParams = MyBase.CreateParams
            myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
            Return myCp
        End Get
    End Property
End Class