如何在vb.net的主表单之前调用子表单

时间:2018-09-10 08:58:36

标签: vb.net visual-studio-2008

在主表单出现之前,我需要我的应用程序首先运行此子表单。

以下是我的代码: Application.myapp

<?xml version="1.0" encoding="utf-8"?>
<MyApplicationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MySubMain>true</MySubMain>
  <MainForm>MainForm</MainForm>
  <SingleInstance>false</SingleInstance>
  <ShutdownMode>0</ShutdownMode>
  <EnableVisualStyles>true</EnableVisualStyles>
  <AuthenticationMode>0</AuthenticationMode>
  <SaveMySettingsOnExit>true</SaveMySettingsOnExit>
</MyApplicationData>

Program.vb(我的子窗体方法在其中) 导入许可证表格

Module Program
    Public Sub Main()

        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New Licenseform.Licenseform)

    End Sub
End Module

当我尝试构建应用程序时,出现以下错误: enter image description here

但是,如果我通过设置应用程序文件来禁用子表单

<mySubMain>false</MySubMain>

它构建成功。 知道有什么问题吗,我应该如何解决?

1 个答案:

答案 0 :(得分:0)

无需禁用应用程序框架并编写自己的Main方法。只需处理应用程序的Startup事件,您可以通过单击项目属性的“应用程序”页面上的“查看应用程序事件”按钮来访问该事件。您可以在那里以与其他任何位置相同的方式显示模式对话。如果将e.Cancel设置为True,则该应用程序将退出,而无需创建启动表单。

作为一个例子,我刚刚创建了一个新的WinForms项目并添加了第二个表单。在该表单上,我添加了两个Buttons,然后添加了以下代码:

Public Class Form2

    Private Sub okButton_Click(sender As Object, e As EventArgs) Handles okButton.Click
        DialogResult = DialogResult.OK
    End Sub

    Private Sub cancelButton_Click(sender As Object, e As EventArgs) Handles cancelButton.Click
        DialogResult = DialogResult.Cancel
    End Sub

End Class

然后我在前面提到的项目属性中使用了按钮,并添加了以下代码:

Imports Microsoft.VisualBasic.ApplicationServices

Namespace My

    ' The following events are available for MyApplication:
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
            Dim dialogue As New Form2

            e.Cancel = (dialogue.ShowDialog() = DialogResult.Cancel)
        End Sub

    End Class

End Namespace

我运行项目时,即使Form2是启动表单,也首先显示Form1。如果单击“确定”按钮,则Form2被关闭,Form1正常打开。如果我单击“取消”按钮,则应用程序退出而未显示Form1