Articles in this section
Category / Section

How to restrict close option of specific MDIChild in DockingManager?

1 min read

In DockingManager, it is possible to restrict the close option of specific MDIChild by handling DockVisibilityChanging and DockVisibilityChanged event.

 

For Example,

 

Here we have restricted the close option for Output window. The following code demonstrates the same.

 

C#

/// <summary>
/// Here we have specified Output window as it cannot be closed. But, when closing Main Form all ChildMDIForm needs to be closed. So, it is handled with the help of this flag property.
/// </summary>
 
bool canCloseOutputMDI;
 
/// <summary>
/// Occurs when MDIChildForm is closed.
/// </summary>
 
public event EventHandler MDIChildFormClosed;
 
//For MDIChildForm closing
 
this.MDIChildFormClosed += ChildFormClosed;
 
//Raising when Changing the state of DockVisibility
 
this.dockingManager1.DockVisibilityChanging += DockingManager1_DockVisibilityChanging1;
 
//Raising after changed the state of DockVisibility changed
 
this.dockingManager1.DockVisibilityChanged += DockingManager1_DockVisibilityChanged;
 
/// <summary>
/// Occurs when Form is loaded
/// </summary>
private void Form1_Load(object sender, EventArgs e)
{
this.dockingManager1.LockDockPanelsUpdate();
this.dockingManager1.SetAsMDIChild(this.Solution, true);
this.dockingManager1.SetAsMDIChild(this.ToolBox, true);
this.dockingManager1.SetAsMDIChild(this.Property, true);
this.dockingManager1.SetAsMDIChild(this.Output, true);
this.dockingManager1.UnlockDockPanelsUpdate();
}
 
 
/// <summary>
/// Invoked when DockVisibility is Changed
/// </summary>
 
private void DockingManager1_DockVisibilityChanged(object sender, DockVisibilityChangedEventArgs arg)
{
this.RaiseMDIChildFormClosed(arg.Control);
}
 
/// <summary>
/// Invoked when DockVisibility is Changing
/// </summary>
 
private void DockingManager1_DockVisibilityChanging1(object sender, DockVisibilityChangingEventArgs arg)
{
//Restrict close option for Output MDIChild window
if (!canCloseOutputMDI && this.dockingManager1.IsMDIMode(arg.Control) && this.dockingManager1.GetDockLabel(arg.Control) == "Output")
arg.Cancel = true;
canCloseOutputMDI = false;
}
 
/// <summary>
/// Invoked when before Form is closing
/// </summary>
/// <param name="e"></param>
 
protected override void OnFormClosing(FormClosingEventArgs e)
{
foreach (var item in this.dockingManager1.ControlsArray)
{
canCloseOutputMDI = true;
if (this.dockingManager1.GetDockVisibility(item))
this.dockingManager1.SetDockVisibility(item, false);
}
e.Cancel = false;
base.OnFormClosing(e);
}
 
/// <summary>
/// This will MDIChildFormClosed event
/// </summary>
public void RaiseMDIChildFormClosed(Control control)
{
if (this.MDIChildFormClosed != null)
{      
this.MDIChildFormClosed(control, new EventArgs());
}
}
 
/// <summary>
/// This event will be triggered when MDIChild in DockingManager is closed
/// </summary>
private void ChildFormClosed(object sender, EventArgs e)
{
if (sender is Control)
{
MessageBox.Show((sender as Control).Name + " : Form Closed");
}
}

 

Screenshot

 

Showing closed window in docking manager

Figure 1: Property Window can be closed.

 

Showing not closing window in docking manager

Figure 2: Output window cannot be closed.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied