Prevent duplicate MDI children forms

前端 未结 7 746
攒了一身酷
攒了一身酷 2020-12-09 11:47

Is there a way to prevent the opening of a certain form within an MDI container if that said form is already opened?

7条回答
  •  臣服心动
    2020-12-09 12:01

    AFAIK there is no standard way. You'll have to implement it yourself. I'd do it this way:

    class TheForm: Form
    {
        private static TheForm Instance;
    
        private TheForm() // Constructor is private
        {
        }
    
        public static Show(Form mdiParent)
        {
            if ( Instance == null )
            {
                // Create new form, assign it to Instance
            }
            else
                Instance.Activate(); // Not sure about this line, find the appropriate equivalent yourself.
        }
    
        protected override OnFormClose(EventArgs e)
        {
            Instance = null;
            base.OnFormClose(e);
        }
    }
    

    If thread safety is of concern, add the appropriate locks.

提交回复
热议问题