视觉基础.根据变量的值更改代码中的默认启动形式 [英] Visual basic. Change default startup form in code, depending on value of variable

查看:49
本文介绍了视觉基础.根据变量的值更改代码中的默认启动形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Visual Studio中的Visual Basic项目上工作,遇到了问题.

I have been working on a Visual Basic project in Visual Studio and have encountered a problem.

我知道可以将项目设计器的应用程序"页面中的启动表单"属性更改为默认表单,但是我需要一种方法,通过ApplicationEvents.vb中的代码来完成此操作,具体取决于其中的变量值应用程序设置.

I understand that the Startup form property in the Application page of the Project Designer can be changed to a default form, however what I require is a way to do this through code in ApplicationEvents.vb depending on the value of a variable within application settings.

目标是,如果用户填写表格,则将值分配给变量,例如变量用户名="xxx".如果此值为true,则默认启动为登录表单(因为用户已注册),如果为false,则将用户带至注册表单.

The goal is that if a user completes a form then a value is assigned to a variable, e.g. variable username = "xxx". If this value is true, then the default startup is a login form (as the user has already registered), and if it is false then the user is taken to a register form.

我很高兴可以使用另一种形式来确定它,但是这似乎使我浪费了ApplicationEvents的功能,并且没有正确使用它(我还想避免空白表单不可避免地闪烁,因为它决定了).

I appreciate that I could use another form to determine this, however this seems like I would be squandering the capabilities of ApplicationEvents and not using it correctly (I also want to avoid the inevitable flicker of a blank form as it decides).

我知道默认格式存储在Application.myapp中,但是,随着.exe的最终发布,此文件(大概)将不会随它一起导出,因此,我想避免直接写入该文件.我也已经读入windowsformsapplicationbase.mainform属性,但是无法弄清楚如何使用它?

I know that the default form is stored in Application.myapp, however with the final publication of the .exe this file will (presumably) not be exported with it, so I want to avoid writing directly to it. I have also read into the windowsformsapplicationbase.mainform property, however cannot figure out how to use it?

这是来自ApplicationEvents.vb的示例代码,用于演示我的问题.

Here is a example piece of code from ApplicationEvents.vb to demonstrate my question.

 If String.IsNullOrEmpty(My.Settings.username) Then
     MsgBox("You have not registered")
     'set register as default form
 Else
     MsgBox("You have registered")
     'set login as default form
 End If

推荐答案

通常,如果您需要对启动时发生的事情进行大量控制,则只想禁用应用程序框架.为此,只需取消选中我的项目设置设计器窗口的应用程序选项卡中的启用应用程序框架复选框.取消选中该复选框后,您就可以将启动对象更改为 Sub Main .然后,您可以使用 Main 方法添加一个新模块,如下所示:

Usually, if you need that much control over what happens at start-up, you just want to disable the a application framework. To do so, just un-check the Enable application framework check-box in the Application tab of the My Project settings designer window. Once you un-check that, you will be able to change the Startup object to Sub Main. Then you can add a new module with a Main method, like this:

Module Module1
    Public Sub Main()
        Application.EnableVisualStyles()
        If String.IsNullOrEmpty(My.Settings.username) Then
            Application.Run(New RegisterForm())
        Else
            Application.Run(New LoginForm())
        End If
    End Sub
End Module

但是请注意,通过禁用应用程序框架,您将失去它提供的其他自动功能,例如 ApplicationEvents .如果要使用应用程序框架,只需在 MyApplication.Startup 事件中设置 MyApplication.MainForm 属性即可完成相同的操作:

Be aware, however--by disabling the application framework, you will loose the other automatic functionality that it provides, such as ApplicationEvents. If you want to use the application framework, you can accomplish the same thing by simply setting the MyApplication.MainForm property in the MyApplication.Startup event:

Partial Friend Class MyApplication
    Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
        If String.IsNullOrEmpty(My.Settings.username) Then
            Me.MainForm = New RegisterForm()
        Else
            Me.MainForm = New LoginForm()
        End If
    End Sub
End Class

或者,您总是可以显示相同的表单,但是该表单只包含一个 UserControl .然后,您可以根据设置简单地切换显示哪个 UserControl .用户控件需要包括所有原本放在两种不同表单上的控件.

Alternatively, you could always show the same form, but then have the form contain nothing but a single UserControl. Then you can simply switch which UserControl is displayed depending upon the settings. The user-controls would need to include all of the controls that would have otherwise been placed on the two different forms.

这篇关于视觉基础.根据变量的值更改代码中的默认启动形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆