Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use Application.Run in Windows Forms Applications

In the context of Windows Forms applications, Application.Run is a crucial method used to start a standard application message loop on the current thread and make a form visible. This method is essential for initializing and running a Windows Forms application, ensuring that the application remains responsive to user inputs and system messages.


Understanding how to utilize Application.Run is important for developers working with Windows Forms because it sets up the necessary environment for the application to operate correctly. This article will provide an overview of Application.Run, its significance, and practical examples to illustrate its usage in Windows Forms applications.


Examples:


1. Basic Example of Application.Run:


```csharp
using System;
using System.Windows.Forms;

namespace MyWindowsFormsApp
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}

public class MainForm : Form
{
public MainForm()
{
Text = "Main Form";
Width = 800;
Height = 600;
}
}
}
```

In this example:
- `Application.EnableVisualStyles()` enables visual styles for the application.
- `Application.SetCompatibleTextRenderingDefault(false)` sets the default text rendering to be compatible with the current operating system.
- `Application.Run(new MainForm())` starts the application with `MainForm` as the main form.

2. Running Multiple Forms:


```csharp
using System;
using System.Windows.Forms;

namespace MyWindowsFormsApp
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

MainForm mainForm = new MainForm();
SecondaryForm secondaryForm = new SecondaryForm();

mainForm.Show();
secondaryForm.Show();

Application.Run();
}
}

public class MainForm : Form
{
public MainForm()
{
Text = "Main Form";
Width = 800;
Height = 600;
}
}

public class SecondaryForm : Form
{
public SecondaryForm()
{
Text = "Secondary Form";
Width = 600;
Height = 400;
}
}
}
```

In this example:
- Two forms, `MainForm` and `SecondaryForm`, are created and shown.
- `Application.Run()` is called without arguments, which keeps the application running until all forms are closed.

3. Handling Form Closure:


```csharp
using System;
using System.Windows.Forms;

namespace MyWindowsFormsApp
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

MainForm mainForm = new MainForm();
mainForm.FormClosed += (s, e) => Application.Exit();

Application.Run(mainForm);
}
}

public class MainForm : Form
{
public MainForm()
{
Text = "Main Form";
Width = 800;
Height = 600;
}
}
}
```

In this example:
- The main form's `FormClosed` event is handled to call `Application.Exit()`, ensuring the application exits when the main form is closed.

To share Download PDF