Question Dispose Objects inside win form within selected tab when closed

dv2020

Active member
Joined
Dec 18, 2020
Messages
30
Programming Experience
1-3
Hi All,

Found this forum recently which looks great. This is my first post.

I have an application I'm working on which loads a new tab within a tab control when a user selects a date from a data grid.
  1. When they select a date, e.g 1/1/2020 a new tab opens within the tabcontrol and the text for that tab is set to 1/1/2020.
  2. User can open multiple dates, which opens multiple tabs with different dates
  3. Inside each tab, it loads a new instance of an existing winform.
  4. Inside that win form, we have a a bit of code.
C#:
public void disposeAllReports()
{
    if (this.crystalReportViewer1 != null) { this.crystalReportViewer1.Dispose();  }
    if (this.cryRpt != null) { this.cryRpt.Close(); }
    if (this.cryRptSum != null) { this.cryRptSum.Dispose(); }

    if (this.crystalReportViewerProfileSum != null) { this.crystalReportViewerProfileSum.Dispose();  }
    if (this.cryRptSum != null) { this.cryRptSum.Close(); }
    if (this.cryRptSum != null) { this.cryRptSum.Dispose(); }

    if (this._dbConnectRead.checkConnection() == true) { this._dbConnectRead.CloseConnection(); }
    if (this._dbConnectSave.checkConnection() == true) { this._dbConnectSave.CloseConnection(); }
    GC.Collect(); // I forgot this line
}

I can not get this code to execute when a user closes that specific tab e.g 1/1/2020.

I have this code, which removes the selected tab 1/1/2020 which works well, but for that spefic winform instance loaded inside the select tab, it wont run the disposeALLreports();
this.tabReportModules.TabPages.Remove(selectedTabPage);

The tabpages.remove does not trigger the form closing trigger either.

I'm a bit stuck on this one, any ideas?

Thanks in advance

David
 
Last edited by a moderator:
Solution
The most basic option that springs to mind is:
C#:
selectedTabPage.Controls.OfType<YourFormType>().Single().disposeAllReports();
before calling Remove. If you are calling that method inside the form already when it's closed, call Close instead, which will dispose the form and thus call that method. You may even just be able to call Dispose on the TabPage and have that dispose the form it contains, but I'm not sure of that without testing.
The most basic option that springs to mind is:
C#:
selectedTabPage.Controls.OfType<YourFormType>().Single().disposeAllReports();
before calling Remove. If you are calling that method inside the form already when it's closed, call Close instead, which will dispose the form and thus call that method. You may even just be able to call Dispose on the TabPage and have that dispose the form it contains, but I'm not sure of that without testing.
 
Solution
Please take not of how I have used CODE and ICODE (inline code) tags in the post above and do the same in your own in future. ICODE tags are not intended for large blocks of code.
 
The most basic option that springs to mind is:
C#:
selectedTabPage.Controls.OfType<YourFormType>().Single().disposeAllReports();
before calling Remove. If you are calling that method inside the form already when it's closed, call Close instead, which will dispose the form and thus call that method. You may even just be able to call Dispose on the TabPage and have that dispose the form it contains, but I'm not sure of that without testing.
Thank you, that worked first Go and did exactly what is required.

I replaced "YourFormType" with the winform name, opened a few multiple tabs, and checked the windows temp folder each time i closed each tab and it was disposing the items correctly, without impacting the other shared tabs.

Thanks Again,

Regards

David
 

Latest posts

Back
Top Bottom