Application-Klasse
Stellt static-Methoden und Eigenschaften für die Verwaltung einer Anwendung zur Verfügung, z. B. Methoden zum Starten und Beenden einer Anwendung, zum Verarbeiten von Windows-Meldungen sowie Eigenschaften für das Abrufen von Informationen zu einer Anwendung. Die Klasse kann nicht geerbt werden.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)
Syntax
'Declaration
Public NotInheritable Class Application
'Usage
Dim instance As Application
public sealed class Application
public ref class Application sealed
public final class Application
public final class Application
Hinweise
Die Application-Klasse verfügt über die folgenden Methoden zum Starten und Beenden von Anwendungen und Threads und zum Verarbeiten von Windows-Meldungen:
Run startet eine Meldungsschleife einer Anwendung für den aktuellen Thread und zeigt wahlweise ein Formular an.
Exit oder ExitThread beendet eine Meldungsschleife.
DoEvents verarbeitet Meldungen, während sich das Programm in einer Schleife befindet.
AddMessageFilter fügt dem Nachrichtensystem der Anwendung für die Überwachung von Windows-Meldungen einen Meldungsfilter hinzu.
Mit IMessageFilter können Sie das Auslösen eines Ereignisses beenden oder bestimmte Vorgänge durchführen, bevor ein Ereignishandler aufgerufen wird.
Diese Klasse verfügt über CurrentCulture-Eigenschaften und CurrentInputLanguage-Eigenschaften zum Abrufen oder Festlegen von Kulturinformationen für den aktuellen Thread.
Sie können keine Instanz dieser Klasse erstellen.
Beispiel
Im folgenden Codebeispiel werden in einem Formular Zahlen in einem Listenfeld aufgeführt. Bei jedem Klicken auf button1
wird der Liste durch die Anwendung eine weitere Zahl hinzugefügt.
Die Main
-Methode ruft Run auf, um die Anwendung zu starten, die das Formular, listBox1
und button1
, erstellt. Wenn der Benutzer auf button1
klickt, zeigt die button1_Click
-Methode eine MessageBox an. Wenn der Benutzer in der MessageBox auf No klickt, fügt die button1_Click
-Methode der Liste eine Zahl hinzu. Wenn der Benutzer auf Yes klickt, ruft die Anwendung Exit auf, um alle verbliebenen Meldungen in der Warteschlange zu verarbeiten und die Anwendung daraufhin zu beenden.
Hinweis
Der Aufruf von Exit schlägt bei partieller Vertrauenswürdigkeit fehl.
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
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 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 );
}
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
Vererbungshierarchie
System.Object
System.Windows.Forms.Application
Threadsicherheit
Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0