Share via


Form-Klasse

Stellt ein Fenster oder ein Dialogfeld dar, das die Benutzeroberfläche einer Anwendung bildet.

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class Form
    Inherits ContainerControl
'Usage
Dim instance As Form
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class Form : ContainerControl
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class Form : public ContainerControl
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class Form extends ContainerControl
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class Form extends ContainerControl

Hinweise

Eine Form ist eine Darstellung eines beliebigen Fensters, das in der Anwendung angezeigt wird. Mit der Form-Klasse können Standardfenster, Toolfenster, rahmenlose und unverankerte Fenster erstellt werden. Sie können auch die Form-Klasse verwenden, um modale Fenster, z. B. ein Dialogfeld, zu erstellen. Eine besondere Art von Formular ist das Multiple Document Interface (MDI)-Formular. Dieses kann andere Formulare enthalten, die als untergeordnete MDI-Formulare bezeichnet werden. Ein MDI-Formular wird erstellt, indem die IsMdiContainer-Eigenschaft auf true festgelegt wird. Untergeordnete MDI-Formulare werden durch Festlegen der MdiParent-Eigenschaft auf das übergeordnete MDI-Formular erstellt, in dem das untergeordnete Formular enthalten sein soll.

Mithilfe der in der Form-Klasse verfügbaren Eigenschaften können Sie die Features für Darstellung, Größe, Farbe und Fensterverwaltung des Fensters bzw. Dialogfelds bestimmen, das Sie erstellen. Mit der Text-Eigenschaft können Sie die Beschriftung des Fensters auf der Titelleiste angeben. Mit der Size-Eigenschaft und der DesktopLocation-Eigenschaft können Sie die Größe und die Position des Fensters definieren, wenn es angezeigt wird. Sie können die ForeColor-Farbeigenschaft verwenden, um die Standardvordergrundfarbe aller im Formular positionierten Steuerelemente zu ändern. Mit den Eigenschaften FormBorderStyle, MinimizeBox und MaximizeBox können Sie steuern, ob das Formular zur Laufzeit minimiert bzw. maximiert oder in seiner Größe verändert werden kann.

Zusätzlich zu den Eigenschaften können Sie die Methoden der Klasse verwenden, um ein Formular zu bearbeiten. Beispielsweise können Sie mit der ShowDialog-Methode ein Formular als modales Dialogfeld anzeigen. Sie können mithilfe der SetDesktopLocation-Methode die Position des Formulars auf dem Desktop festlegen.

Die Ereignisse der Form-Klasse ermöglichen es Ihnen, auf Aktionen zu reagieren, die auf dem Formular ausgeführt werden. Mit dem Activated-Ereignis können Sie Vorgänge wie das Aktualisieren der in den Steuerelementen des Formulars angezeigten Daten ausführen, wenn das Formular aktiviert ist.

Sie können ein Formular in Ihrer Anwendung als Startklasse verwenden, indem Sie eine Methode mit dem Namen Main in die Klasse aufnehmen. Fügen Sie in der Main-Methode Code hinzu, um das Formular zu erstellen und anzuzeigen. Sie müssen der Main-Methode auch das STAThread-Attribut hinzufügen, damit das Formular ausgeführt werden kann. Wenn das Startformular geschlossen wird, wird die Anwendung ebenfalls geschlossen.

Beispiel

Im folgenden Codebeispiel wird eine neue Instanz von Form erstellt. Anschließend wird die ShowDialog-Methode aufgerufen, um das Formular als Dialogfeld anzuzeigen. Im Beispiel werden die Eigenschaften FormBorderStyle, AcceptButton, CancelButton, MinimizeBox, MaximizeBox und StartPosition geändert, um dem Formular die Darstellung und Funktionsweise eines Dialogfelds zu verleihen. Im Beispiel wird zudem die Add-Methode der Controls-Auflistung des Formulars verwendet, um zwei Button-Steuerelemente hinzuzufügen. Im Beispiel wird mit der HelpButton-Eigenschaft eine Hilfeschaltfläche auf der Titelleiste des Dialogfelds angezeigt.

Public Sub CreateMyForm()
    ' Create a new instance of the form.
    Dim form1 As New Form()
    ' Create two buttons to use as the accept and cancel buttons.
    Dim button1 As New Button()
    Dim button2 As New Button()
       
    ' Set the text of button1 to "OK".
    button1.Text = "OK"
    ' Set the position of the button on the form.
    button1.Location = New Point(10, 10)
    ' Set the text of button2 to "Cancel".
    button2.Text = "Cancel"
    ' Set the position of the button based on the location of button1.
    button2.Location = _
       New Point(button1.Left, button1.Height + button1.Top + 10)
    ' Set the caption bar text of the form.   
    form1.Text = "My Dialog Box"
    ' Display a help button on the form.
    form1.HelpButton = True
       
    ' Define the border style of the form to a dialog box.
    form1.FormBorderStyle = FormBorderStyle.FixedDialog
    ' Set the MaximizeBox to false to remove the maximize box.
    form1.MaximizeBox = False
    ' Set the MinimizeBox to false to remove the minimize box.
    form1.MinimizeBox = False
    ' Set the accept button of the form to button1.
    form1.AcceptButton = button1
    ' Set the cancel button of the form to button2.
    form1.CancelButton = button2
    ' Set the start position of the form to the center of the screen.
    form1.StartPosition = FormStartPosition.CenterScreen
       
    ' Add button1 to the form.
    form1.Controls.Add(button1)
    ' Add button2 to the form.
    form1.Controls.Add(button2)
       
    ' Display the form as a modal dialog box.
    form1.ShowDialog()
End Sub
public void CreateMyForm()
{
   // Create a new instance of the form.
   Form form1 = new Form();
   // Create two buttons to use as the accept and cancel buttons.
   Button button1 = new Button ();
   Button button2 = new Button ();
  
   // Set the text of button1 to "OK".
   button1.Text = "OK";
   // Set the position of the button on the form.
   button1.Location = new Point (10, 10);
   // Set the text of button2 to "Cancel".
   button2.Text = "Cancel";
   // Set the position of the button based on the location of button1.
   button2.Location
      = new Point (button1.Left, button1.Height + button1.Top + 10);
   // Set the caption bar text of the form.   
   form1.Text = "My Dialog Box";
   // Display a help button on the form.
   form1.HelpButton = true;

   // Define the border style of the form to a dialog box.
   form1.FormBorderStyle = FormBorderStyle.FixedDialog;
   // Set the MaximizeBox to false to remove the maximize box.
   form1.MaximizeBox = false;
   // Set the MinimizeBox to false to remove the minimize box.
   form1.MinimizeBox = false;
   // Set the accept button of the form to button1.
   form1.AcceptButton = button1;
   // Set the cancel button of the form to button2.
   form1.CancelButton = button2;
   // Set the start position of the form to the center of the screen.
   form1.StartPosition = FormStartPosition.CenterScreen;
   
   // Add button1 to the form.
   form1.Controls.Add(button1);
   // Add button2 to the form.
   form1.Controls.Add(button2);
   
   // Display the form as a modal dialog box.
   form1.ShowDialog();
}
public:
   void CreateMyForm()
   {
      // Create a new instance of the form.
      Form^ form1 = gcnew Form;
      // Create two buttons to use as the accept and cancel buttons.
      Button^ button1 = gcnew Button;
      Button^ button2 = gcnew Button;
      
      // Set the text of button1 to "OK".
      button1->Text = "OK";
      // Set the position of the button on the form.
      button1->Location = Point(10,10);
      // Set the text of button2 to "Cancel".
      button2->Text = "Cancel";
      // Set the position of the button based on the location of button1.
      button2->Location =
         Point( button1->Left, button1->Height + button1->Top + 10 );
      // Set the caption bar text of the form.   
      form1->Text = "My Dialog Box";
      // Display a help button on the form.
      form1->HelpButton = true;
      
      // Define the border style of the form to a dialog box.
      form1->FormBorderStyle = ::FormBorderStyle::FixedDialog;
      // Set the MaximizeBox to false to remove the maximize box.
      form1->MaximizeBox = false;      
      // Set the MinimizeBox to false to remove the minimize box.
      form1->MinimizeBox = false;
      // Set the accept button of the form to button1.
      form1->AcceptButton = button1;
      // Set the cancel button of the form to button2.
      form1->CancelButton = button2;
      // Set the start position of the form to the center of the screen.
      form1->StartPosition = FormStartPosition::CenterScreen;
      
      // Add button1 to the form.
      form1->Controls->Add( button1 );
      // Add button2 to the form.
      form1->Controls->Add( button2 );
      // Display the form as a modal dialog box.
      form1->ShowDialog();
   }
public void CreateMyForm()
{
    // Create a new instance of the form.
    Form form1 = new Form();

    // Create two buttons to use as the accept and cancel buttons.
    Button button1 = new Button();
    Button button2 = new Button();

    // Set the text of button1 to "OK".
    button1.set_Text("OK");

    // Set the position of the button on the form.
    button1.set_Location(new Point(10, 10));

    // Set the text of button2 to "Cancel".
    button2.set_Text("Cancel");

    // Set the position of the button based on the location of button1.
    button2.set_Location(new Point(button1.get_Left(), 
        button1.get_Height() + button1.get_Top() + 10));

    // Set the caption bar text of the form.   
    form1.set_Text("My Dialog Box");

    // Display a help button on the form.
    form1.set_HelpButton(true);

    // Define the border style of the form to a dialog box.
    form1.set_FormBorderStyle(get_FormBorderStyle().FixedDialog);

    // Set the MaximizeBox to false to remove the maximize box.
    form1.set_MaximizeBox(false);

    // Set the MinimizeBox to false to remove the minimize box.
    form1.set_MinimizeBox(false);

    // Set the accept button of the form to button1.
    form1.set_AcceptButton(button1);

    // Set the cancel button of the form to button2.
    form1.set_CancelButton(button2);

    // Set the start position of the form to the center of the screen.
    form1.set_StartPosition(FormStartPosition.CenterScreen);

    // Add button1 to the form.
    form1.get_Controls().Add(button1);

    // Add button2 to the form.
    form1.get_Controls().Add(button2);

    // Display the form as a modal dialog box.
    form1.ShowDialog();
} //CreateMyForm

Vererbungshierarchie

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ScrollableControl
           System.Windows.Forms.ContainerControl
            System.Windows.Forms.Form
               Abgeleitete Klassen

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

Siehe auch

Referenz

Form-Member
System.Windows.Forms-Namespace