Looking at other people's methods, they are all cleverly implemented from the syntax perspective.
What I want to achieve is dialogform. Show ();
Click the button to display the dialog box form. If the form is not closed, click it again.
I used a stupid method, that is, using a static class to save data in the memory. Whether the record form is displayed. Class Cglobal { Static Isshow = False ;}
Judge before dialogform. Show.If(Cglobal. isshow=False)
{Dialogform. Show ();}
After the form is displayedForm_load () {cglobal. isshow=True;}
The method is not elegant, but easy to understand, haha.
URL: http://greatverve.cnblogs.com/archive/2011/06/28/csharp-one-form.html
Reference 1: C # Only one Program Instance Using System;
Using System. Windows. forms;
Using System. runtime. interopservices; // Required to use dllimport.
Using System. diagnostics; // Introduce process class
Namespace Namespace
{
Static Class Program
{
Private Const Int Ws_shownormal = 1 ;
[Dllimport ( " User32.dll " )]
Private Static Extern Bool Showwindowasync (intptr hwnd, Int Cmdshow );
[Dllimport ( " User32.dll " )]
Private Static Extern Bool Setforegroundwindow (intptr hwnd );
/// <Summary>
/// The main entry point of the application.
/// </Summary>
[Stathread]
Static Void Main ()
{
Process instance = Getrunninginstance ();
If (Instance = Null )
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault ( False );
Application. Run ( New Frm_main ()); // Start the main form here.
}
Else
{
Handlerunninginstance (instance );
}
}
/// <Summary>
/// Obtain whether the current process has the same status.
/// </Summary>
/// <Returns> </returns>
Public Static Process getrunninginstance ()
{
Process current = Process. getcurrentprocess ();
Process [] Processes = Process. getprocessesbyname (current. processname );
// Traversing a routine with the same name running
Foreach (Process In Processes)
{
// Ignore existing routines
If (Process. ID ! = Current. ID)
// Ensure that the routine runs from the EXE file
If (System. reflection. Assembly. getexecutingassembly (). Location. Replace ( " / " , " \\ " ) = Current. mainmodule. filename)
Return Process;
}
Return Null ;
}
/// <Summary>
/// Activate the original process.
/// </Summary>
/// <Param name = "instance"> </param>
Public Static Void Handlerunninginstance (process instance)
{
Showwindowasync (instance. main1_whandle, ws_shownormal );
Setforegroundwindow (instance. main1_whandle );
}
}
}
Reference 2: how to generate only one class instance in C #
Sometimes we encounter this situation: in a form, we click a button to generate a new form.CodeIf you click the button again, a form is generated. For each form, an instance of the form corresponds to it, what if I want to allow only one class instance to be generated in a program? In fact, the above implementation can not generate a new form is relatively simple implementation, you can set the button attribute to unavailable after the form is generated: button1.enable = false;
However, what I want to achieve here is that you do not need to make the button unavailable to complete the method that the program can only generate one class instance:
Method 1:
If you only want one instance to operate on a class, you can define all the fields, attributes, and functions in the class as static, and define the constructors as private, in this way, only the class name can access the fields, attributes, and methods in the class, and the class cannot create an instance outside the class.
Public Class Class2
{
Private Class2 () {}
Public Static Type variablename;
Public Static Type functionname () {}
}
This method is not suitable for the form class in the winform program.
Method 2:
Set a Boolean variable to identify whether an instance has been created.
Public Class Class2
{
Private Class2 ()
{
}
PrivateStatic Bool Instance_flag = False ;
Public Static Class2 getinstance ()
{
If ( ! Instance_flag)
{
Class2 C2=NewClass2 ();
Instance_flag=True;
ReturnC2;
}
Else
{
Console. writeline ("You have already create a instance");
Return Null;
}
}
}
If you have created an instanceInstance_flag = true, so that the program will know that an instance has been created and no new instance will be created.
Method 3:
Create a class instance in class2. The instance is static, and then assign values directly to the instance outside the class. You can also return the class instance through a function. However, this class instance will be generated during compilation. It will waste resources and the efficiency is not high. It can be generated only when instances are needed, the Code is as follows:
Public Class Class2
{
Private Class2 ()
{
}
Private Static Class2 C2 = Null ;
Public Static Class2 getinstance ()
{
If (C2 = Null )
{
C2=NewClass2 ();
}
Return C2;
}
}
The static method can only access static domains. According to the above code, you can use the class name to call the getinstance () method when you need to use an instance. To return an instance.
The constructors cannot be used in the preceding methods, because they are all set to private, so is the purpose. Multiple instance generation is prohibited. Thank you!