Exportar (0) Imprimir
Expandir todo

LayoutEventArgs (Clase)

Proporciona datos para el evento Layout. Esta clase no se puede heredar.

Espacio de nombres: System.Windows.Forms
Ensamblado: System.Windows.Forms (en system.windows.forms.dll)

public sealed class LayoutEventArgs : EventArgs
public final class LayoutEventArgs extends EventArgs
public final class LayoutEventArgs extends EventArgs
No aplicable.

Los cambios realizados en un control, como cambiar el tamaño, mostrar u ocultar controles secundarios y agregar o quitar controles secundarios, hacen necesario que un control diseñe sus propios controles secundarios. LayoutEventArgs especifica el control secundario que se ha cambiado y la propiedad que resulta afectada. Por ejemplo, si se ha hecho visible un control desde la última operación de diseño, se verá afectada la propiedad Visible.

Las propiedades AffectedControl y AffectedProperty se establecen en referencia null (Nothing en Visual Basic) si no se proporcionan valores cuando se llama al método PerformLayout.

Para obtener más información sobre la forma de controlar eventos, vea Utilizar eventos.

El ejemplo de código siguiente utiliza las propiedades AffectedProperty y AffectedControl como argumentos de LayoutEventHandler para coordinar el tamaño de un formulario Windows Forms y sus controles con distintos cambios de propiedad realizados tanto en el propio formulario Windows Forms como en los controles.

public class Form1 : System.Windows.Forms.Form
{
   private System.Windows.Forms.TextBox textBox1;
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.Button layoutButton;
   private System.ComponentModel.Container components = null;

   public Form1()
   {
      InitializeComponent();
   }

   protected override void Dispose( bool disposing )
   {
      if( disposing )
      {
         if (components != null) 
         {
            components.Dispose();
         }
      }
      base.Dispose( disposing );
   }

   private void InitializeComponent()
   {
      this.layoutButton = new System.Windows.Forms.Button();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.label1 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // layoutButton
      // 
      this.layoutButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
      this.layoutButton.Location = new System.Drawing.Point(72, 88);
      this.layoutButton.Name = "layoutButton";
      this.layoutButton.Size = new System.Drawing.Size(150, 23);
      this.layoutButton.TabIndex = 0;
      this.layoutButton.Text = "Hello";
      // 
      // textBox1
      // 
      this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
         | System.Windows.Forms.AnchorStyles.Right);
      this.textBox1.Location = new System.Drawing.Point(24, 40);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(248, 20);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "Hello";
      this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(24, 16);
      this.label1.Name = "label1";
      this.label1.TabIndex = 2;
      this.label1.Text = "Button\'s Text:";
      // 
      // Form1
      // 
      this.ClientSize = new System.Drawing.Size(292, 129);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                   this.label1,
                                                                   this.textBox1,
                                                                   this.layoutButton});
      this.Name = "Form1";
      this.Text = "Layout Sample";
      this.Layout += new System.Windows.Forms.LayoutEventHandler(this.Form1_Layout);
      this.ResumeLayout(false);

   }

   [STAThread]
   static void Main() 
   {
      Application.Run(new Form1());
   }
   
   // This method ensures that the form's width is the preferred size of 300 pixels
   // or the size of the button plus 50 pixels, whichever amount is less.
   private void Form1_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
   {
      // This event is raised once at startup with the AffectedControl
      // and AffectedProperty properties on the LayoutEventArgs as null. 
      // The event provides size preferences for that case.
      if ((e.AffectedControl != null) && (e.AffectedProperty != null))
      {
         // Ensure that the affected property is the Bounds property
         // of the form.
         if (e.AffectedProperty.ToString() == "Bounds") 
         {
            // If layoutButton's width plus a padding of 50 pixels is greater than the preferred 
            // size of 300 pixels, increase the form's width.
            if ((this.layoutButton.Width + 50) > 300) 
            {
               this.Width = this.layoutButton.Width + 50;
            }
               // If not, keep the form's width at 300 pixels.
            else 
            {
               this.Width = 300;
            }

            // Center layoutButton on the form.
            this.layoutButton.Left = (this.ClientSize.Width - this.layoutButton.Width) / 2;
         }
      }
   }

   // This method sets the Text property of layoutButton to the Text property
   // of textBox1.  If the new text plus a padding of 20 pixels is larger than 
   // the preferred size of 150 pixels, increase layoutButton's Width property.
   private void textBox1_TextChanged(object sender, System.EventArgs e)
   {
      // Set the Text property of layoutButton.
      this.layoutButton.Text = this.textBox1.Text;
      // Get the width of the text using the proper font.
      int textWidth = (int)this.CreateGraphics().MeasureString(layoutButton.Text, layoutButton.Font).Width;

      // If the width of the text plus a padding of 20 pixels is greater than the preferred size of
      // 150 pixels, increase layoutButton's width.
      if ((textWidth + 20) > 150)
      {
         // Setting the size property on any control raises 
         // the Layout event for its container.
         this.layoutButton.Width = textWidth + 20;
      }
         // If not, keep layoutButton's width at 150 pixels.
      else 
      {
         this.layoutButton.Width = 150;
      }
   }
}

public class Form1 extends System.Windows.Forms.Form
{
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button layoutButton;
    private System.ComponentModel.Container components = null;

    public Form1()
    {
        InitializeComponent();
    } //Form1

    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }
        }
        super.Dispose(disposing);
    } //Dispose

    private void InitializeComponent()
    {
        this.layoutButton = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        //
        // layoutButton
        //
        this.layoutButton.set_Anchor(System.Windows.Forms.AnchorStyles.Bottom);
        this.layoutButton.set_Location(new System.Drawing.Point(72, 88));
        this.layoutButton.set_Name("layoutButton");
        this.layoutButton.set_Size(new System.Drawing.Size(150, 23));
        this.layoutButton.set_TabIndex(0);
        this.layoutButton.set_Text("Hello");
        //
        // textBox1
        //
        this.textBox1.set_Anchor(System.Windows.Forms.AnchorStyles.Top 
            | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.
            AnchorStyles.Right);
        this.textBox1.set_Location(new System.Drawing.Point(24, 40));
        this.textBox1.set_Name("textBox1");
        this.textBox1.set_Size(new System.Drawing.Size(248, 20));
        this.textBox1.set_TabIndex(1);
        this.textBox1.set_Text("Hello");
        this.textBox1.add_TextChanged(new System.EventHandler(this.
            textBox1_TextChanged));
        //
        // label1
        //
        this.label1.set_Location(new System.Drawing.Point(24, 16));
        this.label1.set_Name("label1");
        this.label1.set_TabIndex(2);
        this.label1.set_Text("Button\'s Text:");
        //
        // Form1
        //
        this.set_ClientSize(new System.Drawing.Size(292, 129));
        this.get_Controls().AddRange(new System.Windows.Forms.Control[] { this.
            label1, this.textBox1, this.layoutButton });
        this.set_Name("Form1");
        this.set_Text("Layout Sample");
        this.add_Layout(new System.Windows.Forms.LayoutEventHandler(this.
            Form1_Layout));
        this.ResumeLayout(false);
    } //InitializeComponent

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main

   // This method ensures that the form's width is the preferred size of 300 pixels
   // or the size of the button plus 50 pixels, whichever amount is less.
    private void Form1_Layout(Object sender,
        System.Windows.Forms.LayoutEventArgs e)
    {
      // This event is raised once at startup with the AffectedControl
      // and AffectedProperty properties on the LayoutEventArgs as null. 
      // The event provides size preferences for that case.
        if (e.get_AffectedControl() != null 
            && e.get_AffectedProperty() != null) {
            // Ensure that the affected property is the Bounds property
            // of the form.
            if ((e.get_AffectedProperty().ToString()).Equals("Bounds")) {
            // If layoutButton's width plus a padding of 50 pixels is greater 
            // than the preferred size of 300 pixels, increase the form's width.
                if (this.layoutButton.get_Width() + 50 > 300) {
                    this.set_Width(this.layoutButton.get_Width() + 50);
                }
                    // If not, keep the form's width at 300 pixels.
                else {
                    this.set_Width(300);
                }

                // Center layoutButton on the form.
                this.layoutButton.set_Left((this.get_ClientSize().get_Width() 
                    - this.layoutButton.get_Width()) / 2);
            }
        }
    } //Form1_Layout

    // This method sets the Text property of layoutButton to the Text property
    // of textBox1.  If the new text plus a padding of 20 pixels is larger than 
    // the preferred size of 150 pixels, increase layoutButton's Width property.
    private void textBox1_TextChanged(Object sender, System.EventArgs e)
    {
        // Set the Text property of layoutButton.
        this.layoutButton.set_Text(this.textBox1.get_Text());
        // Get the width of the text using the proper font.
        int textWidth = (int)(this.CreateGraphics().MeasureString(layoutButton.
            get_Text(), layoutButton.get_Font()).get_Width());

        // If the width of the text plus a padding of 20 pixels is greater than the preferred size of
        // 150 pixels, increase layoutButton's width.
        if (textWidth + 20 > 150){
            // Setting the size property on any control raises 
            // the Layout event for its container.
            this.layoutButton.set_Width(textWidth + 20);
        }
            // If not, keep layoutButton's width at 150 pixels.
        else{
            this.layoutButton.set_Width(150);
        }
    } //textBox1_TextChanged
} //Form1

System.Object
   System.EventArgs
    System.Windows.Forms.LayoutEventArgs

Los miembros estáticos públicos (Shared en Visual Basic) 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 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0, 2.0, 1.1, 1.0
Mostrar:
© 2014 Microsoft