Skip to main content

How to: Perform Actions On Application Startup

  • 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.

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.

    VB-CustomCodeOnAppStartup-Method1-01-DisableAppFramework

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

    Public Class Form1
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1)  'Specify the startup form
        End Sub
    End Class
    
  4. Insert the code to be executed before the Application.Run method call.

    Public Class Form1
        Shared Sub Main()
            'Add your code here
            DevExpress.XtraEditors.WindowsFormsSettings.ForceDirectXPaint()
            DevExpress.XtraEditors.WindowsFormsSettings.EnableFormSkins()
    
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1)  'Specify the startup form
        End Sub
    End Class
    

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.

    VB-CustomCodeOnAppStartup-01-ViewAppEvents.png

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

    VB-CustomCodeOnAppStartup-02-SubscribeStartup.gif

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

    Imports DevExpress.XtraEditors
    Imports Microsoft.VisualBasic.ApplicationServices
    
    Namespace My
        ' The following events are available for MyApplication:
        ' Startup: Raised when the application starts, before the startup form is created.
        ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
        ' UnhandledException: Raised if the application encounters an unhandled exception.
        ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
        ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
        Partial Friend Class MyApplication
            Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
                'Add your code here
                WindowsFormsSettings.ForceDirectXPaint()
                WindowsFormsSettings.EnableFormSkins()
            End Sub
        End Class
    End Namespace