C# Windows Forms Application class

Step by step Application class Example in C#

This tutorial will help you learn the following concepts:

  1. Learn C# Application class

Example 1: Simplest Application Example

Start Windows Forms learning by creating and running your first Form in C#.

This example will comprise the following files:

  • Application.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project

Step 2: Write Code

Write Code as follows:

*(a). Application.cs

Create a file named Application.cs

Import the System.Windows.Forms:

using System.Windows.Forms;

Create a namespace called Examples:

namespace Examples {

Create a simple class called MainClass:

  class MainClass {

Typically, the Main() is the main entry point for C# applications. void return type implies we won't return any value.

    public static void Main() {

Inside it we will invoke the Application.Run() and pass in the instance of the Form class. The Application class is a sealed class that provides static methods and properties to manage our application

      Application.Run(new Form());

Here is the full code

using System.Windows.Forms;
namespace Examples {
  class MainClass {
    public static void Main() {
      Application.Run(new Form());
    }
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example 2: C# Application.EnableVisualStyles Example

Learn Application.EnableVisualStyles through examples

This example will comprise the following files:

  • ApplicationEnableVisualStyle.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project

Step 2: Write Code

Write Code as follows:

*(a). ApplicationEnableVisualStyle.cs

Create a file named ApplicationEnableVisualStyle.cs

Add System.Windows.Forms as an import

using System.Windows.Forms;

Create our namespace

namespace Examples {

Extend the Form class

  class Form1 : Form {

 The main entry point for the application.

```csharp
    public static void Main() {

Then invoke Application.EnableVisualStyles() before running the application. The EnableVisualStyles() enables the visual styles of the application, just as it says. Try to Comment it to see the difference (Windows only).

      Application.EnableVisualStyles();

Then run the application

      Application.Run(new Form1());

Here is our form properties

    public Form1() {
      this.button1.Location = new System.Drawing.Point(24, 16);
      this.button1.Size = new System.Drawing.Size(120, 100);
      this.button1.Text = "I am themed.";

      // Sets up how the form should be displayed and adds the controls to the form.
      this.ClientSize = new System.Drawing.Size(300, 286);
      this.Controls.Add(this.button1);

      this.Text = "Application::EnableVisualStyles Example";
    }

Here is the full code


using System.Windows.Forms;
//end
namespace Examples {
    //end
  class Form1 : Form {
        //end
        //Instantiate a Button as an instance field:
    private Button button1 = new Button();
//end
    public static void Main() {
        //end
      Application.EnableVisualStyles();
      //end
      Application.Run(new Form1());
      //end
    }
    public Form1() {
      this.button1.Location = new System.Drawing.Point(24, 16);
      this.button1.Size = new System.Drawing.Size(120, 100);
      this.button1.Text = "I am themed.";

      // Sets up how the form should be displayed and adds the controls to the form.
      this.ClientSize = new System.Drawing.Size(300, 286);
      this.Controls.Add(this.button1);

      this.Text = "Application::EnableVisualStyles Example";
    }
    //end
  };
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example: C# Application.Idle

Simple Application.Idle Example

This example will comprise the following files:

  • ApplicationIdle.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). ApplicationIdle.cs

Create a file named ApplicationIdle.cs

Add two imports: System and System.Windows.Forms:

using System;
using System.Windows.Forms;

Create our namespace

namespace Examples {

Inherit the Form class.

  class Form1 : Form {

define an integer as an instance field:

    private int counter = 0;

Define our main method:

    public static void Main() {
      Application.Run(new Form1());
    }

Set the Idle property:

    Form1() {
      Application.Idle += OnApplicationIdle;
    }

Handle the OnApplicationIdle() event

    private void OnApplicationIdle(object sender, EventArgs e) {
      Text = string.Format("{0}", ++counter);
    }

Here is the full code


using System;
using System.Windows.Forms;
namespace Examples {
  class Form1 : Form {
    private int counter = 0;

    public static void Main() {
      Application.Run(new Form1());
    }

    Form1() {
      Application.Idle += OnApplicationIdle;
    }
    private void OnApplicationIdle(object sender, EventArgs e) {
      Text = string.Format("{0}", ++counter);
    }

  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts