This is a migrated thread and some comments may be shown as answers.

MDI Form, avoid duplicate opened forms

3 Answers 431 Views
Dock
This is a migrated thread and some comments may be shown as answers.
Davide
Top achievements
Rank 1
Davide asked on 26 Apr 2012, 02:05 PM

Hi there,

I am currently developing an MDI application exploiting your Raddock Control.

As I am using forms as tabbed windows, I would like my application not to create a new tab (a second one), if the form is already open.

Currently I am using the following workaround and nonetheless I don’t like very much this way J


private void AddTabbedForm(BaseForm form)
{
    if (this.radDock1.DockWindows.DocumentWindows.Where(w => w.Text == form.Text).Count() > 0)
    {
        this.radDock1.ActivateWindow(this.radDock1.DockWindows.DocumentWindows.Where(w => w.Text == form.Text).First());
        return;
    }
      form.MdiParent = this;
    form.Show();
}
 


Any hint to have it implemented in a more elegant way?


Thank you very much
Davide

3 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 26 Apr 2012, 03:08 PM
Hi Davide, 

I would make the forms that you wish to display singleton classes and then not only can you show just the single instance, but you can also focus on the correct form as well. 

There are a few parts to this... 
The Main Form
Designer File
namespace RadControlsWinFormsApp1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radButton1 = new Telerik.WinControls.UI.RadButton();
            this.radButton2 = new Telerik.WinControls.UI.RadButton();
            this.radDock1 = new Telerik.WinControls.UI.Docking.RadDock();
            this.documentContainer1 = new Telerik.WinControls.UI.Docking.DocumentContainer();
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radButton2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radDock1)).BeginInit();
            this.radDock1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.documentContainer1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            //
            // radButton1
            //
            this.radButton1.Dock = System.Windows.Forms.DockStyle.Top;
            this.radButton1.Location = new System.Drawing.Point(0, 0);
            this.radButton1.Name = "radButton1";
            this.radButton1.Size = new System.Drawing.Size(467, 24);
            this.radButton1.TabIndex = 0;
            this.radButton1.Text = "Launch MDI Form 1";
            this.radButton1.Click += new System.EventHandler(this.radButton1_Click);
            //
            // radButton2
            //
            this.radButton2.Dock = System.Windows.Forms.DockStyle.Top;
            this.radButton2.Location = new System.Drawing.Point(0, 24);
            this.radButton2.Name = "radButton2";
            this.radButton2.Size = new System.Drawing.Size(467, 24);
            this.radButton2.TabIndex = 1;
            this.radButton2.Text = "Launch MDI Form 2";
            this.radButton2.Click += new System.EventHandler(this.radButton2_Click);
            //
            // radDock1
            //
            this.radDock1.Controls.Add(this.documentContainer1);
            this.radDock1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.radDock1.DocumentManager.DocumentInsertOrder = Telerik.WinControls.UI.Docking.DockWindowInsertOrder.InFront;
            this.radDock1.Location = new System.Drawing.Point(0, 48);
            this.radDock1.MainDocumentContainer = this.documentContainer1;
            this.radDock1.Name = "radDock1";
            this.radDock1.Padding = new System.Windows.Forms.Padding(5);
            //
            //
            //
            this.radDock1.RootElement.MinSize = new System.Drawing.Size(25, 25);
            this.radDock1.Size = new System.Drawing.Size(467, 421);
            this.radDock1.SplitterWidth = 4;
            this.radDock1.TabIndex = 2;
            this.radDock1.TabStop = false;
            this.radDock1.Text = "radDock1";
            //
            // documentContainer1
            //
            this.documentContainer1.Location = new System.Drawing.Point(5, 5);
            this.documentContainer1.Name = "documentContainer1";
            this.documentContainer1.Padding = new System.Windows.Forms.Padding(5);
            //
            //
            //
            this.documentContainer1.RootElement.MinSize = new System.Drawing.Size(25, 25);
            this.documentContainer1.Size = new System.Drawing.Size(457, 411);
            this.documentContainer1.SizeInfo.SizeMode = Telerik.WinControls.UI.Docking.SplitPanelSizeMode.Fill;
            this.documentContainer1.SplitterWidth = 4;
            this.documentContainer1.TabIndex = 0;
            this.documentContainer1.TabStop = false;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(467, 469);
            this.Controls.Add(this.radDock1);
            this.Controls.Add(this.radButton2);
            this.Controls.Add(this.radButton1);
            this.Name = "Form1";
            //
            //
            //
            this.RootElement.ApplyShapeToControl = true;
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radButton2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radDock1)).EndInit();
            this.radDock1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.documentContainer1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private Telerik.WinControls.UI.RadButton radButton1;
        private Telerik.WinControls.UI.RadButton radButton2;
        private Telerik.WinControls.UI.Docking.RadDock radDock1;
        private Telerik.WinControls.UI.Docking.DocumentContainer documentContainer1;
 
 
    }
}

Main Form 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
 
namespace RadControlsWinFormsApp1
{
    public partial class Form1 : RadForm
    {
        public Form1()
        {
            InitializeComponent();
            this.IsMdiContainer = true;
            this.radDock1.AutoDetectMdiChildren = true;
        }
 
 
        private void radButton1_Click(object sender, EventArgs e)
        {
            MdiChild1 child1 = MdiChild1.Instance;
            DisplayForm(child1);
        }
 
        private void radButton2_Click(object sender, EventArgs e)
        {
            MdiChild2 child2 = MdiChild2.Instance;
            DisplayForm(child2);
        }
 
 
        private void DisplayForm(System.Windows.Forms.Form form)
        {
            this.Cursor = Cursors.WaitCursor;
            if (this.Contains(form))
            {
                Telerik.WinControls.UI.Docking.HostWindow host = (Telerik.WinControls.UI.Docking.HostWindow)form.Parent;
                this.radDock1.ActivateWindow(host);
            }
            else
            {
                form.MdiParent = this;
                form.Show();
                Telerik.WinControls.UI.Docking.HostWindow host = (Telerik.WinControls.UI.Docking.HostWindow)form.Parent;
                this.radDock1.ActivateWindow(host);
            }
 
            form.Focus();
            this.Cursor = Cursors.Default;
        }
 
 
 
    }
}


MDI Child1 Designer
namespace RadControlsWinFormsApp1
{
    partial class MdiChild1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            //
            // MdiChild1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(298, 279);
            this.Name = "MdiChild1";
            this.Text = "MdiChild1";
            this.ThemeName = "ControlDefault";
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
    }
}

MDIChild1 Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
 
namespace RadControlsWinFormsApp1
{
    public partial class MdiChild1 : Telerik.WinControls.UI.RadForm
    {
        private static MdiChild1 s_Instance;
 
        private MdiChild1()
        {
            InitializeComponent();
            this.FormClosed += new FormClosedEventHandler(MdiChild1_FormClosed);
        }
 
        public static MdiChild1 Instance
        {
            get
            {
                if (MdiChild1.s_Instance == null)
                {
                    MdiChild1.s_Instance = new MdiChild1();
                }
                return MdiChild1.s_Instance;
            }
        }
 
        void MdiChild1_FormClosed(object sender, FormClosedEventArgs e)
        {
            s_Instance = null;
        }
 
 
    }
}

(for MDI Child 2, just copy MDIChild1, replacing MDIChild1 with MDIChild2)

Hope this helps. 
Please remember to mark as answer if this solves your issue, or let me know if you have further questions
Richard
0
Davide
Top achievements
Rank 1
answered on 26 Apr 2012, 03:47 PM
Hi Richard,
  thank you for the good answer, I like this  Singleton approach, but now we have already more than thirty developed forms,
and time to finish application it's not so much :-) , if there was anything that would allow me to avoid to touch all  forms would be better


Davide
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 26 Apr 2012, 04:16 PM
Hi Davide, 

In that case, your current workaround would probably be fine. Though, to alter 30 forms to singleton shouldn't take too long. 
Regards, 
Richard
Tags
Dock
Asked by
Davide
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Davide
Top achievements
Rank 1
Share this question
or