简体   繁体   中英

Forms in VB.Net

I am from ac# background and am converting a vb.net windows forms app to c#. I have a windows form called associateForm. In code the developer references associate form like so:-

Private Sub NotifyIcon1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles EMS.MouseDoubleClick
    If e.Button = Windows.Forms.MouseButtons.Left Then

        If associateForm.WindowState = FormWindowState.Normal And associateForm.Visible = True Then
            associateForm.WindowState = FormWindowState.Minimized
        Else
            associateForm.WindowState = FormWindowState.Normal
        End If

        associateForm.Show()
        associateForm.Focus()
        'bring to front of other windows
        associateForm.Activate()
    End If
End Sub

But the thing is that associateForm is not instantiated within the class where the method is being executed. Nor is the class static. Nor does there seem to be an instance of the class anywhere in code. Can anyone shine any light on why this seems to work but when i try this in C# it is not having any of it.

VB.Net for .Net 2.0 and later has something called default form instances. When you define a form, you get an automatic instance of the form with the same name as the type.

Several things are wrong with the previous comments. Joel's is basically the correct answer, with one small caveat:

When you define a form, you get an automatic instance of the form with the same name as the type.

You actually only get the instance when you call it. Before that, no default instance is created. Thus, instance creation is deferred until the time of usage.

Using the name of the form as an instance is actually a shortcut to My.Forms.Formname , which is a compiler-generated list of properties for all forms. These properties are responsible for the object creation. It is unfortunate that Microsoft has decided to make these properties implicitly available at global scope (hence the OP's question).

This feature exists because VB.NET emulates the VB6 method of being able to refer to a default instance of a form through it's name.

Well. While this feature is inspired by previous versions of VB, it's not a direct descended. VB 7 didn't have it. Rather, it was re-introduced with VB 8 (= VB 2005).

A really really stupid "feature"

Actually, it's a dead useful feature that plays very well with VB's RAD development philosophy. As mentioned above, it's unfortunate that such a loose scoping is employed. But other than that, I'd be very interested in hearing what's so “stupid” about this feature.

Joel is correct. This feature exists because VB.NET emulates the VB6 method of being able to refer to a default instance of a form through it's name.

When you convert this to C# you need to provide global access to aa single instance named associateForm. There is a chance that something is preserved in between calls to this form. After you successfully gotten it working this way. Then you can refactor the software and test to see if a local instantiation will work the same.

I can't answer your question directly, but...

I'm guessing you're converting from VB.Net to C# manually - have you tried using an automatic convertor instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM