How can I ensure that a Form that has been docked to an MDIForm
doesn't get docked a 2nd time? That is, I need a Form to only be
visible once. I tried making it a singleton class, but calling the
DockControl of the RadDock still added a same form , though one
was an empty shell.
8 Answers, 1 is accepted
0

Richard Slade
Top achievements
Rank 2
answered on 24 Feb 2011, 10:46 AM
Hello Meraj,
Please have a look at this forum post which explains a few options in detail.
Hope that helps
Richard
Please have a look at this forum post which explains a few options in detail.
Hope that helps
Richard
0

meraj
Top achievements
Rank 1
answered on 26 Feb 2011, 03:19 PM
Hi Richard,
My scenario is that i have a MDI form with menu and i am opening the form as per menu click .
My scenario is that i have a MDI form with menu and i am opening the form as per menu click .
0
Accepted

Richard Slade
Top achievements
Rank 2
answered on 26 Feb 2011, 10:29 PM
Hello Meraj,
Here is a full exmaple for you using a MainForm (MDI Parent) and two child forms.
Hope this helps
Main.Designer.cs
Main.cs
ChildForm.Designer.cs
ChildForm.cs
ChildForm2.Designer.cs
ChildForm2.cs
Let me know if you have any questions
Richard
Here is a full exmaple for you using a MainForm (MDI Parent) and two child forms.
Hope this helps
Main.Designer.cs
namespace
RadControlsWinFormsApp1
{
partial
class
Main
{
/// <summary>
/// Required designer variable.
/// </summary>
private
System.ComponentModel.IContainer components;
/// <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
.radMenu1 =
new
Telerik.WinControls.UI.RadMenu();
this
.radMenuItem1 =
new
Telerik.WinControls.UI.RadMenuItem();
this
.radDock1 =
new
Telerik.WinControls.UI.Docking.RadDock();
this
.documentContainer1 =
new
Telerik.WinControls.UI.Docking.DocumentContainer();
this
.radMenuItem2 =
new
Telerik.WinControls.UI.RadMenuItem();
((System.ComponentModel.ISupportInitialize)(
this
.radMenu1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(
this
.radDock1)).BeginInit();
this
.radDock1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(
this
.documentContainer1)).BeginInit();
this
.SuspendLayout();
//
// radMenu1
//
this
.radMenu1.Items.AddRange(
new
Telerik.WinControls.RadItem[] {
this
.radMenuItem1,
this
.radMenuItem2});
this
.radMenu1.Location =
new
System.Drawing.Point(0, 0);
this
.radMenu1.Name =
"radMenu1"
;
this
.radMenu1.Size =
new
System.Drawing.Size(548, 20);
this
.radMenu1.TabIndex = 1;
this
.radMenu1.Text =
"radMenu1"
;
//
// radMenuItem1
//
this
.radMenuItem1.Name =
"radMenuItem1"
;
this
.radMenuItem1.Text =
"Launch Child Form"
;
this
.radMenuItem1.Click +=
new
System.EventHandler(
this
.radMenuItem1_Click);
//
// radDock1
//
this
.radDock1.AutoDetectMdiChildren =
true
;
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.IsCleanUpTarget =
true
;
this
.radDock1.Location =
new
System.Drawing.Point(0, 20);
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(548, 424);
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.RootElement.MinSize =
new
System.Drawing.Size(25, 25);
this
.documentContainer1.Size =
new
System.Drawing.Size(538, 414);
this
.documentContainer1.SizeInfo.SizeMode = Telerik.WinControls.UI.Docking.SplitPanelSizeMode.Fill;
this
.documentContainer1.SplitterWidth = 4;
this
.documentContainer1.TabIndex = 0;
this
.documentContainer1.TabStop =
false
;
//
// radMenuItem2
//
this
.radMenuItem2.Name =
"radMenuItem2"
;
this
.radMenuItem2.Text =
"Launch Child Form 2"
;
this
.radMenuItem2.Click +=
new
System.EventHandler(
this
.radMenuItem2_Click);
//
// Main
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(548, 444);
this
.Controls.Add(
this
.radDock1);
this
.Controls.Add(
this
.radMenu1);
this
.IsMdiContainer =
true
;
this
.Name =
"Main"
;
this
.Text =
"Form1"
;
this
.Load +=
new
System.EventHandler(
this
.Main_Load);
((System.ComponentModel.ISupportInitialize)(
this
.radMenu1)).EndInit();
((System.ComponentModel.ISupportInitialize)(
this
.radDock1)).EndInit();
this
.radDock1.ResumeLayout(
false
);
((System.ComponentModel.ISupportInitialize)(
this
.documentContainer1)).EndInit();
this
.ResumeLayout(
false
);
this
.PerformLayout();
}
#endregion
private
Telerik.WinControls.UI.RadMenu radMenu1;
private
Telerik.WinControls.UI.RadMenuItem radMenuItem1;
private
Telerik.WinControls.UI.Docking.RadDock radDock1;
private
Telerik.WinControls.UI.Docking.DocumentContainer documentContainer1;
private
Telerik.WinControls.UI.RadMenuItem radMenuItem2;
}
}
Main.cs
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.UI.Docking;
namespace
RadControlsWinFormsApp1
{
public
partial
class
Main : Form
{
public
Main()
{
InitializeComponent();
}
private
void
radMenuItem1_Click(
object
sender, EventArgs e)
{
ChildForm childForm = ChildForm.Instance;
if
(childForm.Parent ==
null
)
{
childForm.MdiParent =
this
;
childForm.Show();
}
else
{
this
.radDock1.ActivateMdiChild(childForm);
}
}
private
void
radMenuItem2_Click(
object
sender, EventArgs e)
{
ChildForm2 childForm2 = ChildForm2.Instance;
if
(childForm2.Parent ==
null
)
{
childForm2.MdiParent =
this
;
childForm2.Show();
}
else
{
this
.radDock1.ActivateMdiChild(childForm2);
}
}
private
void
Main_Load(
object
sender, EventArgs e)
{
}
}
}
ChildForm.Designer.cs
namespace
RadControlsWinFormsApp1
{
partial
class
ChildForm
{
/// <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
.radLabel1 =
new
Telerik.WinControls.UI.RadLabel();
((System.ComponentModel.ISupportInitialize)(
this
.radLabel1)).BeginInit();
this
.SuspendLayout();
//
// radLabel1
//
this
.radLabel1.Location =
new
System.Drawing.Point(13, 13);
this
.radLabel1.Name =
"radLabel1"
;
this
.radLabel1.Size =
new
System.Drawing.Size(99, 16);
this
.radLabel1.TabIndex = 0;
this
.radLabel1.Text =
"I am the child form"
;
//
// ChildForm
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(284, 262);
this
.Controls.Add(
this
.radLabel1);
this
.Name =
"ChildForm"
;
this
.Text =
"ChildForm"
;
this
.FormClosed +=
new
System.Windows.Forms.FormClosedEventHandler(
this
.ChildForm_FormClosed);
((System.ComponentModel.ISupportInitialize)(
this
.radLabel1)).EndInit();
this
.ResumeLayout(
false
);
this
.PerformLayout();
}
#endregion
private
Telerik.WinControls.UI.RadLabel radLabel1;
}
}
ChildForm.cs
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;
namespace
RadControlsWinFormsApp1
{
public
partial
class
ChildForm : Form
{
private
static
ChildForm instance;
private
ChildForm()
{
InitializeComponent();
}
public
static
ChildForm Instance
{
get
{
if
(instance ==
null
)
{
instance =
new
ChildForm();
}
return
instance;
}
}
private
void
ChildForm_FormClosed(
object
sender, FormClosedEventArgs e)
{
instance.Dispose();
instance =
null
;
}
}
}
ChildForm2.Designer.cs
namespace
RadControlsWinFormsApp1
{
partial
class
ChildForm2
{
/// <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
.radLabel1 =
new
Telerik.WinControls.UI.RadLabel();
((System.ComponentModel.ISupportInitialize)(
this
.radLabel1)).BeginInit();
this
.SuspendLayout();
//
// radLabel1
//
this
.radLabel1.Location =
new
System.Drawing.Point(22, 13);
this
.radLabel1.Name =
"radLabel1"
;
this
.radLabel1.Size =
new
System.Drawing.Size(96, 16);
this
.radLabel1.TabIndex = 0;
this
.radLabel1.Text =
"I am Child Form 2"
;
//
// ChildForm2
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(284, 262);
this
.Controls.Add(
this
.radLabel1);
this
.Name =
"ChildForm2"
;
this
.Text =
"ChildForm2"
;
this
.FormClosed +=
new
System.Windows.Forms.FormClosedEventHandler(
this
.ChildForm2_FormClosed);
((System.ComponentModel.ISupportInitialize)(
this
.radLabel1)).EndInit();
this
.ResumeLayout(
false
);
this
.PerformLayout();
}
#endregion
private
Telerik.WinControls.UI.RadLabel radLabel1;
}
}
ChildForm2.cs
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;
namespace
RadControlsWinFormsApp1
{
public
partial
class
ChildForm2 : Form
{
private
static
ChildForm2 instance;
private
ChildForm2()
{
InitializeComponent();
}
public
static
ChildForm2 Instance
{
get
{
if
(instance ==
null
)
{
instance =
new
ChildForm2();
}
return
instance;
}
}
private
void
ChildForm2_FormClosed(
object
sender, FormClosedEventArgs e)
{
instance.Dispose();
instance =
null
;
}
}
}
Let me know if you have any questions
Richard
0

meraj
Top achievements
Rank 1
answered on 27 Feb 2011, 08:27 AM
Thank You Richard,
Its very useful.
Its very useful.
0

Richard Slade
Top achievements
Rank 2
answered on 27 Feb 2011, 09:47 AM
You're welcome. Please remember to mark as answer.
0
Accepted

Emanuel Varga
Top achievements
Rank 1
answered on 27 Feb 2011, 09:59 AM
Hello Meraj, Richard
I would suggest a different approach because you do not always have the luxury to make a singleton out of a form, or in general change the structure of the form,
I believe there is an easier way of doing this, just before adding a new form you can check if that form is already part of the MdiChildren collection and just creating and adding it if it's not, like so:
This way you don't need to make your form a singleton.
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
I would suggest a different approach because you do not always have the luxury to make a singleton out of a form, or in general change the structure of the form,
I believe there is an easier way of doing this, just before adding a new form you can check if that form is already part of the MdiChildren collection and just creating and adding it if it's not, like so:
using
System.Drawing;
using
System.Linq;
using
System.Windows.Forms;
using
Telerik.WinControls.UI;
using
Telerik.WinControls.UI.Docking;
public
class
Form1 : Form
{
private
RadDock radDock1;
private
RadMenu radMenu1;
public
Form1()
{
InitializeComponent();
this
.Size =
new
Size(800, 600);
this
.Controls.Add(radDock1 =
new
RadDock());
radDock1.Dock = DockStyle.Fill;
radDock1.MdiChildrenDockType = DockType.Document;
this
.IsMdiContainer =
true
;
this
.radDock1.AutoDetectMdiChildren =
true
;
this
.Controls.Add(radMenu1 =
new
RadMenu());
radMenu1.Dock = DockStyle.Top;
var menuItem =
new
RadMenuItem(
"Show Form1"
);
menuItem.Click +=
new
System.EventHandler(menuItem_Click);
radMenu1.Items.Add(menuItem);
}
void
menuItem_Click(
object
sender, System.EventArgs e)
{
var form = radDock1.MdiChildren.Where(m => m
is
Form2).FirstOrDefault();
if
(form ==
null
)
{
form =
new
Form2();
form.MdiParent =
this
;
form.Visible =
true
;
}
else
{
radDock1.ActivateMdiChild(form);
}
}
/// <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
.components =
new
System.ComponentModel.Container();
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.Text =
"Form1"
;
}
#endregion Windows Form Designer generated code
}
public
class
Form2 : Form
{
public
Form2()
{
InitializeComponent();
this
.Controls.Add(
new
RadLabel() { Text =
"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
.components =
new
System.ComponentModel.Container();
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.Text =
"Form2"
;
}
#endregion Windows Form Designer generated code
}
This way you don't need to make your form a singleton.
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
0

aquariens
Top achievements
Rank 1
answered on 22 Jun 2011, 09:03 AM
May be a bit late for this post but I achieved the same by comparing .text property of the form and the document by iterating the documents in dock.
Easy calling from menus or RibbonBar
Hope it helps.
Private
Function
IterateDockLoadForm(
ByVal
Form
As
RadForm)
As
Boolean
IterateDockLoadForm =
False
Dim
a = (From c
As
Docking.HostWindow
In
ucMainDock.DocumentManager.DocumentEnumerator
Select
c.Text)
For
i
As
Short
= 0
To
a.Count - 1
If
a(i).ToString = Form.Text
Then
IterateDockLoadForm =
True
End
If
Next
If
IterateDockLoadForm =
True
Then
Return
Nothing
Exit
Function
Else
Dim
hostWindow
As
New
Docking.HostWindow(Form)
ucMainDock.DocumentManager.DocumentInsertOrder = Docking.DockWindowInsertOrder.ToBack
ucMainDock.AddDocument(hostWindow)
ucMainDock.ShowDocumentCloseButton =
True
End
If
End
Function
Easy calling from menus or RibbonBar
IterateDockLoadForm(FormName)
Hope it helps.
0
Hi meraj,
Julian Benkov
the Telerik team
Thank you for sharing your solution with the community. I am sure that the others will benefit from it.
If you have feedback to share or if you have questions, feel free to write back.
Julian Benkov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!