Assembly: System.Windows.Forms (in system.windows.forms.dll)
<ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _ Public Class Form Inherits ContainerControl
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
Un oggetto Form rappresenta una qualsiasi finestra visualizzata nell'applicazione. La classe Form può essere utilizzata per creare finestre standard, finestre degli strumenti, finestre senza bordi e finestre mobili, nonché finestre modali, ad esempio le finestre di dialogo. Un'interfaccia a documenti multipli (MDI) è un tipo speciale di form che può contenere altri form denominati form figlio MDI. Per creare un form MDI è necessario impostare la proprietà IsMdiContainer su true. I form figlio MDI vengono creati impostando la proprietà MdiParent sul form padre MDI che conterrà il form figlio.
Utilizzando le proprietà disponibili nella classe Form, è possibile determinare l'aspetto, la dimensione, il colore e le funzionalità di gestione della finestra o della finestra di dialogo che si sta creando. La proprietà Text consente di specificare la didascalia della finestra nella barra del titolo, mentre le proprietà Size e DesktopLocation consentono di definire la dimensione e la posizione della finestra visualizzata. È possibile utilizzare la proprietà ForeColor per modificare il colore di primo piano predefinito di tutti i controlli inclusi nel form. Le proprietà FormBorderStyle, MinimizeBox e MaximizeBox consentono di controllare se il form può essere ridotto a icona, ingrandito o ridimensionato in fase di esecuzione.
Per modificare un form, oltre alle proprietà è possibile utilizzare i metodi della classe. Ad esempio, è possibile utilizzare il metodo ShowDialog per visualizzare un form come finestra di dialogo modale e il metodo SetDesktopLocation per posizionare il form sul desktop.
Gli eventi della classe Form consentono di rispondere alle azioni eseguite sul form. È possibile utilizzare l'evento Activated per eseguire determinate operazioni, ad esempio l'aggiornamento dei dati visualizzati nei controlli del form al momento dell'attivazione.
Un form può essere utilizzato come classe iniziale nell'applicazione inserendo nella classe un metodo denominato Main, in cui è necessario aggiungere il codice per la creazione e la visualizzazione del form. Per eseguire il form, sarà inoltre necessario aggiungere l'attributo STAThread al metodo Main. La chiusura del form iniziale determina la chiusura dell'applicazione.
Nell’esempio di codice riportato di seguito viene creata una nuova istanza di Form e viene chiamato il metodo ShowDialog per visualizzare il form come finestra di dialogo. Vengono impostate le proprietà FormBorderStyle, AcceptButton, CancelButton, MinimizeBox, MaximizeBox e StartPosition per trasformare l'aspetto e la funzionalità del form in quelli di una finestra di dialogo. Viene inoltre utilizzato il metodo Add dell'insieme Controls del form per aggiungere due controlli Button. Infine viene utilizzata la proprietà HelpButton per visualizzare un pulsante ? sulla barra del titolo della finestra di dialogo.
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
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.ContainerControl
System.Windows.Forms.Form
Classi derivate
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.