开发者

replace default form instance

开发者 https://www.devze.com 2023-02-12 01:36 出处:网络
I am using VB at the moment and VB has an annoying feature called \"Default Form Instance\", which creates a default instance of a form object when you reference the form class instead of form instanc

I am using VB at the moment and VB has an annoying feature called "Default Form Instance", which creates a default instance of a form object when you reference the form class instead of form instance.

ex:

public class frmMain  
  Inherits System.Windows.Forms.Form  
end class  

private Sub Sub1  
  frmMain.Show()  
end Sub  

The code above compiles and runs without an error because runtime gives you a new instance of frmMain when you call it by class name.

The question is:

Is there a way to replace the default instance with an instance that I have created? Another way to put it: Is there a way to set the instance that I cr开发者_StackOverflow中文版eated to be the default instance?

For those who would like to ask "Why on earth would you need something like that ?":

I have this application, let's call it MyApplication.exe, which is a windows forms application and frmMain is the main form. Many references in the application to the main form is through the default instance, which was working fine until now. I am making some changes to the application. Instead of running MyApplication.exe directly, I will have to load the assembly dynamically and run it through reflection. Here is how I do it:

Dim assembly As Reflection.Assembly = LoadAssembly("MyApplication.exe")  
Dim frm As Object = assembly.CreateInstance("MyApplication.frmMain")  
frm.Show()

I create and show an instance of frmMain through reflection. Later on, when the application tries to access frmMain through the default instance, the runtime creates a new frmMain instance because it thinks the default instance is not there yet. But now the default instance and the one on the screen are different objects.

Bottom line is: Through reflection I am trying to mimic the exact behavior of running MyApplication.exe directly.


This cannot be done as evidenced by trying to set the default instance form to something else

    Dim newForm As New frmMain
    My.Forms.frmMain = newForm

This code throws an ArgumentException saying "Property can only be set to Nothing"

You should add a field and then find and replace all default form instance references to explicit references

Private _myfrmMain as new frmMain
private Sub Sub1
_myfrmMain.Show()
end sub


From the PROJECT menu select the last item.

It will be like

WindowsApplication1 Properties...

On the Application TAB under Shutdown Mode change it to

When last Form Closes

Then you can have a new instance of frmMain and close the default Form1 or whatever the default Form is called.

I hope this helps. :-)

Public Class Form1

    Friend WithEvents frmMain As New Form

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

        frmMain.Text = "frmMain"
        frmMain.Show()
        Me.Close()

    End Sub

End Class
0

精彩评论

暂无评论...
验证码 换一张
取 消