Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Application Class
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Application Class

Updated: November 2007

Provides static methods and properties to manage an application, such as methods to start and stop an application, to process Windows messages, and properties to get information about an application. This class cannot be inherited.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Visual Basic (Declaration)
Public NotInheritable Class Application
Visual Basic (Usage)
Dim instance As Application
C#
public sealed class Application
Visual C++
public ref class Application sealed
J#
public final class Application
JScript
public final class Application

The Application class has methods to start and stop applications and threads, and to process Windows messages, as follows:

  • Run starts an application message loop on the current thread and, optionally, makes a form visible.

  • Exit or ExitThread stops a message loop.

  • DoEvents processes messages while your program is in a loop.

  • AddMessageFilter adds a message filter to the application message pump to monitor Windows messages.

  • IMessageFilter lets you stop an event from being raised or perform special operations before invoking an event handler.

This class has CurrentCulture and CurrentInputLanguage properties to get or set culture information for the current thread.

You cannot create an instance of this class.

The following code example lists numbers in a list box on a form. Each time you click button1, the application adds another number to the list.

The Main method calls Run to start the application, which creates the form, listBox1 and button1. When the user clicks button1, the button1_Click method displays a MessageBox. If the user clicks No on the MessageBox, the button1_Click method adds a number to the list. If the user clicks Yes, the application calls Exit to process all remaining messages in the queue and then to quit.

Note:

The call to Exit will fail in partial trust.

Visual Basic
Public Class Form1 
    Inherits Form

    <STAThread()> _
     Shared Sub Main()
        ' Start the application.
        Application.Run(New Form1)
    End Sub

    Private WithEvents button1 As Button
    Private WithEvents listBox1 As ListBox

    Public Sub New()
        button1 = New Button
        button1.Left = 200
        button1.Text = "Exit"

        listBox1 = New ListBox
        Me.Controls.Add(button1)
        Me.Controls.Add(listBox1)
    End Sub

    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles button1.Click
        Dim count As Integer = 1
        ' Check to see whether the user wants to exit the application.
        ' If not, add a number to the list box.
        While (MessageBox.Show("Exit application?", "", _
            MessageBoxButtons.YesNo) = DialogResult.No)

            listBox1.Items.Add(count)
            count += 1

        End While

        ' The user wants to exit the application. 
        ' Close everything down.
        Application.Exit()
    End Sub

End Class

C#
public class Form1 : Form
{
    [STAThread]
    public static void Main()
    {
        // Start the application.
        Application.Run(new Form1());
    }

    private Button button1;
    private ListBox listBox1;

    public Form1()
    {
        button1 = new Button();
        button1.Left = 200;
        button1.Text = "Exit";
        button1.Click += new EventHandler(button1_Click);

        listBox1 = new ListBox();
        this.Controls.Add(button1);
        this.Controls.Add(listBox1);
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        int count = 1;
        // Check to see whether the user wants to exit the application.
        // If not, add a number to the list box.
        while (MessageBox.Show("Exit application?", "", 
            MessageBoxButtons.YesNo)==DialogResult.No)
        {
            listBox1.Items.Add(count);
            count += 1;
        }

        // The user wants to exit the application. 
        // Close everything down.
        Application.Exit();
    }
}

Visual C++
public ref class Form1: public System::Windows::Forms::Form
{
private:
   Button^ button1;
   ListBox^ listBox1;

public:
   Form1()
   {
      button1 = gcnew Button;
      button1->Left = 200;
      button1->Text =  "Exit";
      button1->Click += gcnew EventHandler( this, &Form1::button1_Click );
      listBox1 = gcnew ListBox;
      this->Controls->Add( button1 );
      this->Controls->Add( listBox1 );
   }

private:
   void Form1::button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      int count = 1;

      // Check to see whether the user wants to exit 
      // the application. If not, add a number to the list box.
      while ( MessageBox::Show(  "Exit application?",  "", MessageBoxButtons::YesNo ) == ::DialogResult::No )
      {
         listBox1->Items->Add( count );
         count += 1;
      }


      // The user wants to exit the application. 
      // Close everything down.
      Application::Exit();
   }

};

int main()
{

   // Starts the application.
   Application::Run( gcnew Form1 );
}


J#
public class Form1 extends Form
{
    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        // Start the application.
        Application.Run(new Form1());
    } //main

    private Button button1;
    private ListBox listBox1;

    public Form1()
    {
        button1 = new Button();
        button1.set_Left(200);
        button1.set_Text("Exit");
        button1.add_Click(new EventHandler(button1_Click));
        listBox1 = new ListBox();
        this.get_Controls().Add(button1);
        this.get_Controls().Add(listBox1);
    } //Form1

    public void button1_Click(Object sender, System.EventArgs e)
    {
        int count = 1;

        // Check to see whether the user wants to exit the application.
        // If not, add a number to the list box.
        while ((MessageBox.Show("Exit application?", "", 
                MessageBoxButtons.YesNo).Equals(get_DialogResult().No))) {
            listBox1.get_Items().Add(new Integer(count));
            count += 1;
        }
        // The user wants to exit the application. 
        // Close everything down.
        Application.Exit();
    } //button1_Click
} //Form1

System..::.Object
  System.Windows.Forms..::.Application
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker