何时创建默认表单实例? [英] When are default form instances created?

查看:125
本文介绍了何时创建默认表单实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET中,表单实例的启动顺序和处理非常奇怪。当你引用一个表单例如 Form2.Textbox1.Text =Foo时,CLR会自动使用在背景中创建的表单实例,并且可以直接通过 My.Forms.Form2

使用WinForms应用程序,通过自定义 Sub Main()。这里的应用程序是通过调用 Application.Run(frmMain)



来运行的。由于我有多个需要初始化我想知道真正的表单实例是在哪一点创建的。它们全部是在 Application.Run 中创建的,还是仅在我调用 Form2.Show()时才创建?我的启动顺序如下:
$ b


  1. Sub Main()

  2. Sub Main()调用 Application.Run(frmMain)

  3. frmMain.Load 调用 frmNotMain.Show()

我现在可以找到像 My.Forms.frmNotMain =新的frmNotMain ,所以它并不明显在哪里创建实例。



预先感谢您。

解决方案

根据MSDN


当您通过My访问表单时。 Forms.Form1,工厂方法检查Form1的实例是否已经打开。如果是,则返回该实例。


所以基本上它被创建并且 Sub New code>就在它被显示之前被调用(不是在某个地方创建并保存直到需要)。链接包括这段代码,显示它如何创建这些默认实例:

 '代码...由编译器生成
公共m_Form1作为Form1
公共财产Form1()作为Form1
获得
m_Form1 = Create__Instance__(的Form1)(m_Form1)
返回m_Form1
结束获取
Set(ByVal Value As Form1)
如果Value是m_Form1
返回
End If
如果Not Value是Nothing Then
抛出新的ArgumentException(属性只能设置)
End If
Dispose__Instance__(Of Form1)(m_Form1)
End Set
End Property

然而,你正在谈论默认的(怪异)实例方法,这是不明智的开始。这在很大程度上是为了提供与VB6类型代码的兼容性,您只需执行 myForm.Show()来实例化并显示一个表单(并且可能适用于那些不真正了解实例的调试者或OOP)。

表单是类,应该通过显式创建实例来对待;所以,一般情况下:

  Dim frm As New frmMain'NEW创建实例
frm.Show

您可以在窗体的<$ c中为 InitializeComponent 设置一个断点$ c> Sub New 来查看它何时被调用。要创建一个全局引用,像你可以用任何其他类一样:

  Friend frmMain As MainForm'no instance yet 
Friend myMain As MainClass

Public Sub Main

'在创建任何表单前执行此操作
Application.EnableVisualStyles()

myMain = new MainClass()
myMain.DoStuff()
$ b frmMain = New MainForm()'instanced(NEW)

Application.Run(frmMain)
结束小组

同样:

<$ p $ fr $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'创建实例

frm2.Show

在所有情况下,<$ c $当您使用 New 运算符创建新表单时,将调用表单的c> Sub New 。 VB试图通过重复使用 New 来清除这个问题,但默认情况下所有实际隐藏在表单工厂中的。


The startup sequence and handling of form instances is quite weird in VB.NET. When you refer to a form as for example Form2.Textbox1.Text = "Foo" then the CLR automatically uses the instance of the form that is created in the background and can be directly acessed through My.Forms.Form2.

I am using a WinForms application that starts up through a custom Sub Main(). Here the application is run by calling Application.Run(frmMain).

Since I have multiple forms that needs initilializing I would like to know exactly at which point the real form instances are created. Are they all created at Application.Run or are they only created when I call Form2.Show()? My startup sequence is as follows right now:

  1. Sub Main()
  2. Sub Main() calls Application.Run(frmMain)
  3. frmMain.Load calls frmNotMain.Show()

I can nowwhere find a line like My.Forms.frmNotMain = New frmNotMain, so it's not apparent where the instance is created.

Thank you in advance.

解决方案

According to MSDN:

When you access Form through My.Forms.Form1, the factory method checks to see if an instance of Form1 is already open. If it is, that instance is returned. Otherwise, an instance of Form1 is created and returned.

So essentially it is created and Sub New called just before it is shown (not created somewhere and held until needed). The link includes this code showing how it creates those default instances:

'Code ... generated by the compiler
Public m_Form1 As Form1
Public Property Form1() As Form1
    Get
        m_Form1 = Create__Instance__ (Of Form1)(m_Form1) 
        Return m_Form1 
    End Get
    Set(ByVal Value As Form1)
     If Value Is m_Form1
    Return
       End If
       If Not Value Is Nothing Then
          Throw New ArgumentException("Property can only be set to Nothing.")
       End If
       Dispose__Instance__ (Of Form1)(m_Form1)
    End Set
End Property

However, you are talking about the default ("weird") instance method which is ill-advised to begin with. This largely exists to provide compatibility with VB6 type code where you did just do myForm.Show() to instance and show a form (and probably for tinkerers who do not really understand instancing or OOP).

Forms are classes and should be treated as such by explicitly creating instances; so, generally:

Dim frm As New frmMain                ' NEW creates the instance
frm.Show

You can set a breakpoint on InitializeComponent in the form's Sub New to see when it is invoked. To create a global reference to it, like you might with any other class:

Friend frmMain As MainForm          ' no instance yet
Friend myMain As MainClass

Public Sub Main

    ' do this before any forms are created
    Application.EnableVisualStyles()

    myMain = New MainClass()
    myMain.DoStuff()

    frmMain = New MainForm()         ' instanced (NEW)

    Application.Run(frmMain)
End Sub

Likewise:

Dim frm2 = New frmNotMain       ' declare and instance

' short for:
Dim frm2 As frmNotMain          ' declare  frm2
frm2 = New frmNotMain           ' create instance

frm2.Show 

In all cases, Sub New for your form(s) would be called when you use the New operator to create a New form. VB tries to make this clear thru the repeated use of New, but with the default instance all that is actually tucked away in the form factory.

这篇关于何时创建默认表单实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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