Singleton mode -- Implementing multiple document windows (MDI)

Keywords: Windows

Use MS visual studio to design a multi document window (MDI), then create a toolbar (ToolStrip), add a button in the toolbar, click the button will pop up a "tools" window, use singleton mode to design, so that only one "tool" window can pop up

1. Design a multi document window: create a blank solution first, and then add MDI class

Right click an item - select Add - New Item - MDI parent form


2. The created MDI has a toolbar by default, and then add its own buttons, as shown in the figure below


3. Click the tool button to pop up a sub window: tools


Operation of tool buttons

private void Tool_Click(object sender, EventArgs e)
        {
            //Tool tool = multi document window_ Singleton mode Tool.GetTool();
            //tool.MdiParent = this;
            //tool.Show();
            Tool tool = Multi document window_Singleton mode.Tool.GetTool();
            //if (tool == null && tool.IsDisposed)
            //{
            //    tool = multi document window_ Singleton mode Tool.GetTool();
            //}
            tool.MdiParent = this;
            tool.Show();
            tool.Focus();
        }
4. Design the Tool with the lazy man single example, so that only one "Tool" window will pop up
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Multi document window_Singleton mode
{
    public partial class Tool : Form
    {
        private static Tool tool = null;
        // Create a static read-only helper object when the program is running
        private static readonly object syncRoot = new object();

        private Tool()
        {
            InitializeComponent();
        }

        // Double judgment mechanism of double checking and locking for single instance
        public static Tool GetTool()
        {
            // The first judgment is to judge whether the instance exists or not, and then lock it
            if (tool == null)
            {
                // Locked programs allow only one thread access at a time
                lock (syncRoot)
                {
                    // Second judgment
                    if (tool == null)
                    {
                        tool = new Tool();
                    }
                    // Resolve unable to access the released object
                    tool.Disposed += new EventHandler(Tool_Disposed);
                }
            }
            else
            {
                Console.WriteLine("Tool window created, only one window can be created");
            }

            return tool;
        }
        // After closing the child window, it can be opened again
        static void Tool_Disposed(object sender, EventArgs e)
        {
            tool = null;
        }

        //private void Exit_Click(object sender, EventArgs e)
        //{
            //this.Dispose();
        //}
    }
}

Operation results:


5. Start up procedure

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Multi document window_Singleton mode
{
    class Program
    {
        static void Main(string[] args)
        {
            Application.Run(new MDI());
        }
    }
}

Posted by tymlls05 on Thu, 09 Jul 2020 08:50:11 -0700