Run Method (Form)
Collapse the table of content
Expand the table of content

Application.Run Method (Form)

Begins running a standard application message loop on the current thread, and makes the specified form visible.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public static void Run (
	Form mainForm
)
public static void Run (
	Form mainForm
)
public static function Run (
	mainForm : Form
)

Parameters

mainForm

A Form that represents the form to make visible.

Exception typeCondition

InvalidOperationException

A main message loop is already running on the current thread.

Typically, the main function of an application calls this method and passes to it the main window of the application.

This method adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application.

NoteNote

The Dispose method of the Form class will be called prior to the return of this method.

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 adds numbers one to three to the list box, and displays a MessageBox. If the user clicks No on the MessageBox, the button1_Click method adds another 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.

The example requires that listBox1 and button1 have been created and placed on a form.

public static void Main(string[] args) {
    // Starts the application.
    Application.Run(new Form1());
 }
 
 private void button1_Click(object sender, System.EventArgs e) {
    // Populates a list box with three numbers.
    int i = 3;
    for(int j=1; j<=i; j++) {
       listBox1.Items.Add(j);
    }
 
    /* Determines whether the user wants to exit the application.
     * If not, adds another number to the list box. */
    while (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) == 
       DialogResult.No) {
       // Increments the counter ands add the number to the list box.
       i++;
       listBox1.Items.Add(i);
    }
 
    // The user wants to exit the application. Close everything down.
    Application.Exit();
 }


public static void main(String[] args)
{
    // Starts the application.
    Application.Run(new Form1());
} //main

protected void button1_Click(Object sender, System.EventArgs e)
{
    // Populates a list box with three numbers.
    int i = 3;
    for (int j = 1; j <= i; j++) {
        listBox1.get_Items().Add(new Integer(j));
    }

    /* Determines whether the user wants to exit the application.
       If not, adds another number to the list box. 
     */
    while ((MessageBox.Show("Exit application?", "", 
        MessageBoxButtons.YesNo).Equals(get_DialogResult().No))) {
        // Increments the counter ands add the number to the list box.
        i++;
        listBox1.get_Items().Add(new Integer(i));
    }

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

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

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft