Application Class
Assembly: System.Windows.Forms (in system.windows.forms.dll)
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. |
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(); } }
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
Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: