Evaluar y enviar comentarios
Contraer todo/Expandir todo Contraer todo
Esta página es específica de
Microsoft Visual Studio 2008/.NET Framework 3.5

Hay además otras versiones disponibles para:
Biblioteca de clases de .NET Framework
Form (Clase)

Actualización: noviembre 2007

Representa una ventana o un cuadro de diálogo que constituye la interfaz de usuario de una aplicación.

Espacio de nombres:  System.Windows.Forms
Ensamblado:  System.Windows.Forms (en System.Windows.Forms.dll)
Visual Basic (Declaración)
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class Form _
    Inherits ContainerControl
Visual Basic (Uso)
Dim instance As Form
C#
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class Form : ContainerControl
Visual C++
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)]
public ref class Form : public ContainerControl
J#
/** @attribute ComVisibleAttribute(true) */
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */
public class Form extends ContainerControl
JScript
public class Form extends ContainerControl

Un formulario Form es una representación de cualquier ventana mostrada en su aplicación. La clase Form se puede utilizar para crear ventanas estándar, de herramientas, sin bordes y flotantes. También puede utilizar la clase Form para crear las ventanas modales como un cuadro de diálogo. Un tipo especial de formulario, el formulario de interfaz de múltiples documentos (MDI), puede contener otros formularios denominados formularios MDI secundarios. Los formularios MDI se crean estableciendo la propiedad IsMdiContainer en true. Los formularios MDI secundarios se crean estableciendo la propiedad MdiParent en el formulario MDI principal que contendrá el formulario secundario.

Utilizando las propiedades disponible en la clase Form, puede determinar el aspecto, tamaño, color y las características de administración de la ventana o cuadro de diálogo que está creando. La propiedad Text le permite especificar el título que aparecerá en la barra de título de la ventana. Las propiedades Size y DesktopLocation le permiten definir el tamaño y la ubicación de la ventana cuando se muestra en la pantalla. Puede utilizar la propiedad de color ForeColor para cambiar el color predeterminado de primer plano de todos los controles incluidos en el formulario. Las propiedades FormBorderStyle, MinimizeBox y MaximizeBox le permiten controlar si se puede minimizar o maximizar el formulario, o si se puede cambiar el tamaño en tiempo de ejecución.

Además de las propiedades, puede utilizar los métodos de la clase para manipular un formulario. Por ejemplo, puede utilizar el método ShowDialog para mostrar un formulario como un cuadro de diálogo modal. El método SetDesktopLocation se puede usar para situar el formulario en el escritorio.

Los eventos de la clase Form le permiten responder a las acciones realizadas en el formulario. Puede utilizar el evento Activated para realizar operaciones como actualizar los datos mostrados en los controles del formulario cuando se activa el formulario.

Puede utilizar un formulario como la clase de inicio de su aplicación colocando un método llamado Main en la clase. En el método Main, agregue el código necesario para crear y mostrar el formulario. También será necesario agregar el atributo STAThread al método Main para que se ejecute el formulario. Cuando se cierra el formulario de inicio, también se cierra la aplicación.

En el ejemplo de código siguiente se crea una instancia nueva de un Form y se llama al método ShowDialog para mostrar el formulario como un cuadro de diálogo. El ejemplo establece las propiedades FormBorderStyle, AcceptButton, CancelButton, MinimizeBox, MaximizeBox y StartPosition para cambiar la apariencia y la funcionalidad del formulario a las de un cuadro de diálogo. En el ejemplo también se utiliza el método Add de la colección Controls del formulario para agregar dos controles Button. En el ejemplo se utiliza la propiedad HelpButton para mostrar un botón de ayuda en la barra de título del cuadro de diálogo.

Visual Basic
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
C#
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();
}
Visual C++
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();
   }
J#
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
Todos los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

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

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

.NET Framework

Compatible con: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 3.5, 2.0, 1.0
Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2012 Microsoft. Reservados todos los derechos. Términos de uso | Marcas Registradas | Privacidad
Page view tracker