Search code examples
.netwinformsshowdelphi-prismsingle-instance

How to display a single instance of a Winform throughout the whole program?


Accept my apology if my question and the description seems too simple or already been asked by others.

I may have asked a similar question before. However, I am still confused a little about winform. Say you have a Delphi prism .net program with Mainform, Form1, Form2, Form3. Plus, you want to be able to display a single instant of a Form1 from within Mainform, Form2 and Form3. How do you do that? I have to have a winform that needs to be displayed as needed throughout the whole program to show application errors within RichTextBox. This means SysErrorDlg winform can be called anytime from anywhere in my program to display program errors. For me to be able to do this is if only single instance of SysErrorDlg winform is accessible throughout my whole program.

follow the very simple code below. That's pretty much what I am trying to do.

=========================================
Mainform

using
Form1;
Form2;
Form3;   

Mainform1 = class(System.Windows.Form)
private
method ShowBtn_Click(sender: System.Object; e: System.EventArgs)
public
constructor;
F1:Form1;
end;

constructor MainForm1;
begin
   F1 := new Form1;
end;

method Mainform1.ShowBtn_Click(sender: System.Object; e: System.EventArgs);
begin
   F1.Show; // or ShowDialog;
end;

=====================================================
Form1

Form1 = class(System.Windows.Form)
private
public
constructor;
end;

constructor Form1;
begin

end;

=====================================================
Form2

using
Mainform;

Form2 = class(System.Windows.Form)
private
method ShowBtn_Click(sender: System.Object; e: System.EventArgs)
public
constructor;
end;

constructor Form2;
begin

end;

method Form2.ShowBtn_Click(sender: System.Object; e: System.EventArgs);
begin
   MainForm1.F1.Show; // or ShowDialog;
end;

====================================================
Form3

Using
MainForm;

Form3 = class(System.Windows.Form)
private
method ShowBtn_Click(sender: System.Object; e: System.EventArgs)
public
constructor;
end;

constructor Form3;
begin

end;

method Form3.ShowBtn_Click(sender: System.Object; e: System.EventArgs);
begin
   MainForm1.F1.Show; // or ShowDialog;
end;

============================================

Is it even possible to do that? My compiler won't let me declare a global winform variable but complains that it needs to be identified to be a public. Even if there is an option to enable this feature, I don't want to for I like the idea of keeping variables private or local to classes.

How do you pass the instance of the form1 to form2 or form3 if an instance of form1 is already declared and instantiated within Mainform? I understand how show and showdialog work. What line of code would you use to access Form1 instance from Form2 if the Form1 instance is within MainForm?

You could provide a little code along with your explanation.


Solution

  • Sounds like you need to use the singleton-pattern for this. For example, in c# I could define Form1 like this:

    public partial class Form1 : Form
    {
        static Form1 _theform;
    
        /// <summary>
        /// Gets the one and only instance of Form1.
        /// </summary>
        static public Form1 TheForm
        {
            get
            {
                if ( null == _theform )
                {
                    _theform = new Form1();
                }
    
                return _theform;
            }
        }
    
        protected Form1()
        {
            InitializeComponent();
        }
    }
    

    Then, anywhere in my application I can get the same instance of Form1 by doing this:

    Form1.TheForm.Show();