Close Application after Closing All RadTabbedForms
Environment
| Product Version | Product | Author |
|---|---|---|
| 2020.2.616 | RadTabbedForm for WinForms | Desislava Yordanova |
Description
RadTabbedForm allows you to display tabs in the title bar, just like in most modern Web browsers. You can pin the tabs or reorder them via drag drop. You can open any tab in a standalone window as well. Thus, by dragging a tab outside the title bar, you will generate another opened form.
However, in case you have multiple windows (forms) opened, each of which can be closed separately via the close button, closing the main window (form) will close the rest of the available forms as well.
A common requirement is to keep the application running until the last form is closed. Although, this scenario is more like a general-programming question, this article is intended to show how to handle it and keep the application running even in case of closing the main form.
Solution
After some research, the following thread seems to be quite useful on this topic and provide different approaches: Close a windows form without exiting the entire application
Usually, the Program file has the following default code:

Application.Run(new RadTabbedForm1());
For VB.NET projects, you can refer to the following thread explaining how to simulate a Program.vb file: What is the equivalent of Program.cs in VB.NET

When your application starts, the first thing it does is to create a new form of whichever form is your main form and then run it. The application will quit when that form is closed. The following code snippet demonstrates how to avoid closing the application when the main form is closed:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var main_form = new RadTabbedForm1();
main_form.Show();
Application.Run();
//comment this code line
//Application.Run(new RadTabbedForm1());
}
Then, you can call Application.Exit when the last opened form is being closed:
public RadTabbedForm1()
{
InitializeComponent();
this.AllowAero = false;
this.FormClosed += RadTabbedForm1_FormClosed;
this.TabbedFormControl.TabbedFormControlElement.ItemDragService.TabbedFormCreating += ItemDragService_TabbedFormCreating;
}
private void RadTabbedForm1_FormClosed(object sender, FormClosedEventArgs e)
{
CloseApplicationIfLast();
}
private void ItemDragService_TabbedFormCreating(object sender, TabbedFormCreatingEventArgs e)
{
e.TabbedForm.FormClosed += TabbedForm_FormClosed;
e.TabbedForm.TabbedFormControlCreating += TabbedForm_TabbedFormControlCreating;
}
private void TabbedForm_TabbedFormControlCreating(object sender, TabbedFormControlCreatingEventArgs e)
{
e.TabbedFormControl.TabbedFormControlElement.ItemDragService.TabbedFormCreating += ItemDragService_TabbedFormCreating;
}
private void TabbedForm_FormClosed(object sender, FormClosedEventArgs e)
{
CloseApplicationIfLast();
}
private void CloseApplicationIfLast()
{
if (Application.OpenForms.Count == 0)
{
Application.Exit();
}
}