How to cancel the close of a specific docked form?

Dear Sir,

I used Dockingmanager with TabbedMDIManager to dock forms as mdichild forms and docked forms. 
1) How to get the form (docked or mdichild) for which the user clicked close button?
2) How to cancel closing docked form and  mdichild form?
3) How to allow to form's closing event to fire when the user click on close button of a docked form or mdichild.

Best regards.

3 Replies

SK Senthil Kumaran Rajan Syncfusion Team September 13, 2018 12:29 PM UTC

Hi Cosyspro, 
 
Thank you for contacting Syncfusion Support. 
 
Query 
Response 
How to get the form (docked or mdichild) for which the user clicked close button? 
 
 
How to cancel closing docked form and  mdichild form? 
 
You can get the closed button pressed form and can cancel the form closing using DockVisibilityChanging event Please make use of the below code snippet for the same. 
 
Code snippet[C#] 
 
/// <summary> 
        /// Invoked when DockVisibility Changed used to find the CloseButton pressed control. 
        /// </summary> 
        private void DockingManager1_DockVisibilityChanging(object sender, DockVisibilityChangingEventArgs arg) 
        { 
            //This is used to restrict the form closing for the both Docked and MDI form. 
            arg.Cancel = true; 
            MessageBox.Show("Closing control: " + (arg.Control as Panel).Name); 
        } 
 
 
How to allow to form's closing event to fire when the user click on close button of a docked form or mdichild. 
You can set the FormClosing event gets fired by invoking the FormClosing event from MdiChildActivate. Please make use of the below code snippet for the same. 
Code snippet[C#] 
 
public Form1() 
        { 
            InitializeComponent(); 
 
            this.MdiChildActivate += Form1_MdiChildActivate; 
 
            this.dockingManager1.DockVisibilityChanging += DockingManager1_DockVisibilityChanging; 
        } 
 
        /// <summary> 
        /// Invoked when the MdiChild Activate 
        /// </summary> 
        private void Form1_MdiChildActivate(object sender, EventArgs e) 
        { 
            this.ActiveMdiChild.FormClosing += ActiveMdiChild_FormClosing1; 
        } 
 
        /// <summary> 
        /// Invoked when the ActiveMdiChild form closing 
        /// </summary> 
        private void ActiveMdiChild_FormClosing1(object sender, FormClosingEventArgs e) 
        { 
            MessageBox.Show("form Closing fired"); 
        } 
 
We have prepared a sample for your reference and it can be downloaded from the below link. 
 
 
 
Please check the above solution let us know if it is useful. 
 
Regards, 
Senthil 



CO Cosyspro September 14, 2018 07:39 AM UTC

Thank you for response,
1) It's working. However, since arg.Cancel = true, the app can't be closing when App's close button is clicked. I need just to cancel the closing of a specific mdi form?
2) In my case, docked and Mdi forms are created at run time. How can I add mdi form's FormClosing method to Form.MdiChildActivate?
Best regards


KR Kannan R Syncfusion Team September 17, 2018 12:00 PM UTC

Hi Cosyspro,  
  
Thank you for your update.  
 
We have modified our sample by handling DockVisibilityChanging and DockVisibilityChanged events to achieve reported requirements. You can use similar approach for both the Docked and MDIChildForms cases. Please find the code snippet in below for your reference.  
 
Code Snippets : [C#] 
 
 
        /// <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) 
        { 
            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 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"); 
            } 
        } 
 
 
Sample: Docking 
 
Please let us know if it helps. 
 
Regards, 
Kannan 


Loader.
Up arrow icon