How to reference mainform in Other templates

Source: Internet
Author: User
Tags dotnet
Providing a reference to an application's main form


Providing a reference to an application's main form
<URL: http://dotnet.mvps.org/dotnet/faqs? Id = accessmainform & lang = en>
----------------------------------------------------------------------------

Providing a reference to an application's main form

Let 'mainform' be the class name of the application's main form. Add
Code below to your project, then go to the Project Settings and change
Startup object to 'sub main ':

///
Public Module Program
Private m_mainform as mainform

Public readonly property mainform () as mainform
Get
Return m_mainform
End get
End Property

Public sub main ()
M_mainform = new mainform ()
Application. Run (m_mainform)
End sub
End Module
///

You can access the main form from everywhere in the app by typing
'Program. mainform '.

-Or-

///
Public class Program
Private shared m_mainform as mainform

Public shared readonly property mainform () as mainform
Get
Return m_mainform
End get
End Property

Public shared sub main ()
M_mainform = new mainform ()
Application. Run (m_mainform)
End sub
End Class
///

There are still some alternatives, for example, you can implement
Singleton Design Pattern for your forms in order to access them by their
Class Name:

Implementing the Singleton pattern in C #
<URL: http://www.yoda.arachsys.com/csharp/singleton.html>

Refreshing ing the singleton Design Pattern
<URL: http://msdn.microsoft.com/library/en-us/dnbda/html/singletondespatt.asp>

Design Pattern: Singleton in C #
<URL: http://www.devhood.com/tutorials/tutorial_details.aspx? Tutorial_id = 486>

Design Patterns: Singleton
<URL: http://www.dofactory.com/Patterns/PatternSingleton.aspx>

My practice is to declare a static class in the shared DLL, which has a static attribute to access and set mainform!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.