Back to Devexpress

How to: Perform Actions On Application Startup

windowsforms-119891-build-an-application-how-to-perform-actions-on-application-startup.md

latest2.5 KB
Original Source

How to: Perform Actions On Application Startup

  • Oct 29, 2020
  • 3 minutes to read

You may need to perform certain actions in code before opening the main application form. For instance, to enable DirectX Hardware Acceleration you need to call the WindowsFormsSettings.ForceDirectXPaint method before the main application form is created.

This topic shows where you can place the application initialization code when developing projects in C# and Visual Basic. If you are a Visual Basic developer, you can choose one of the methods listed below depending on your requirements.

C# Example

For a C# project, locate the Program.cs file in the Solution Explorer. This file contains the static void Main() procedure, in which you can add custom code before the Application.Run method call.

csharp
using DevExpress.XtraEditors;
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            //Add your code here
            WindowsFormsSettings.ForceDirectXPaint();
            WindowsFormsSettings.EnableFormSkins();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Visual Basic Example - Method 1

With this approach, you create the Main function and set it as the application’s entry point.

  1. Right-click your project in the Solution Explorer and select Properties in the context menu.

  2. Uncheck Enable application framework and then set Startup object to Sub Main in the Application tab.

  3. Switch to the your main form’s code editor and manually add the following Shared Sub Main procedure to your form class:

  4. Insert the code to be executed before the Application.Run method call.

Visual Basic Example - Method 2

With this approach, you subscribe to the application’s Startup event to perform custom actions.

  1. Right-click your project in the Solution Explorer and select Properties in the context menu.

  2. Click the View Application Events button in the Application tab.

  3. Subscribe to the Startup event in the ApplicationEvents.vb file that opens.

  4. Insert the code to be executed in the generated Startup event handler.