(0) exportieren Drucken
Alle erweitern

Control.PerformLayout-Methode

Aktualisiert: November 2007

Erzwingt die Anwendung von Layoutlogik durch das Steuerelement auf sämtliche diesem untergeordneten Steuerelemente.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void PerformLayout()
public void PerformLayout()
public function PerformLayout()

Wenn die SuspendLayout-Methode aufgerufen wurde, bevor die PerformLayout-Methode aufgerufen wird, wird das Layout-Ereignis unterdrückt.

Die AffectedControl-Eigenschaft und die AffectedProperty-Eigenschaft der erstellten Instanz der LayoutEventArgs-Klasse werden auf null festgelegt, wenn beim Aufruf der PerformLayout-Methode keine Werte bereitgestellt wurden.

Im folgenden Codebeispiel wird die Verwendung der PerformLayout-Methode veranschaulicht. Außerdem werden verschiedene Verfahren für das Auslösen des Layout-Ereignisses dargestellt. In diesem Beispiel ruft der Click-Ereignishandler für Button1 explizit PerformLayout auf. Der Click-Ereignishandler für Button2 ruft implizit PerformLayout auf. PerformLayout wird auch aufgerufen, wenn das Formular geladen ist. Button3 setzt das Steuerelement auf den Zustand zurück, in dem es sich beim Laden befunden hat. Das Layout-Ereignis wird in allen Fällen ausgelöst.

Dies ist ein vollständiges Beispiel. Fügen Sie zum Ausführen des Beispiels folgenden Code in ein leeres Formular ein.


using System.Windows.Forms;
using System.Drawing;



public class LayoutForm:
	System.Windows.Forms.Form

{
	public LayoutForm() : base()
	{        
		InitializeComponent();
	}

	internal System.Windows.Forms.Button Button1;
	internal System.Windows.Forms.Button Button2;
	internal LayoutControl LayoutControl1;
	internal System.Windows.Forms.Button Button3;

	private void InitializeComponent()
	{
		this.Button1 = new System.Windows.Forms.Button();
		this.Button2 = new System.Windows.Forms.Button();
		this.Button3 = new System.Windows.Forms.Button();
		this.LayoutControl1 = new LayoutControl();
		this.SuspendLayout();
		this.Button1.Location = new System.Drawing.Point(16, 16);
		this.Button1.Name = "Button1";
		this.Button1.Size = new System.Drawing.Size(120, 32);
		this.Button1.TabIndex = 0;
		this.Button1.Text = "Center textbox on control";
		this.Button2.Location = new System.Drawing.Point(152, 16);
		this.Button2.Name = "Button2";
		this.Button2.Size = new System.Drawing.Size(104, 32);
		this.Button2.TabIndex = 3;
		this.Button2.Text = "Shrink user control";
		this.Button3.Location = new System.Drawing.Point(96, 232);
		this.Button3.Name = "Button3";
		this.Button3.TabIndex = 5;
		this.Button3.Text = "Reset";
		this.LayoutControl1.BackColor = System.Drawing.SystemColors.ControlDark;
		this.LayoutControl1.Location = new System.Drawing.Point(72, 64);
		this.LayoutControl1.Name = "LayoutControl1";
		this.LayoutControl1.Size = new System.Drawing.Size(150, 160);
		this.LayoutControl1.TabIndex = 6;
		this.ClientSize = new System.Drawing.Size(292, 266);
		this.Controls.Add(this.Button3);
		this.Controls.Add(this.Button2);
		this.Controls.Add(this.Button1);
		this.Controls.Add(this.LayoutControl1);
		this.Name = "Form1";
		this.Text = "Form1";
		this.ResumeLayout(false);
		this.Button1.Click += new System.EventHandler(Button1_Click);
		this.Button2.Click += new System.EventHandler(Button2_Click);
		this.Button3.Click += new System.EventHandler(Button3_Click);

	}

	[System.STAThread]
	public static void Main()
	{
		Application.Run(new LayoutForm());
	}


	// This method explicitly calls raises the layout event on 
	// LayoutControl1, changing the Bounds property.
	private void Button1_Click(System.Object sender, System.EventArgs e)
	{
		LayoutControl1.PerformLayout(LayoutControl1, "Bounds");
	}

	// This resize of LayoutControl1 implicitly triggers the layout event. 
	//  Changing the size of the control affects its Bounds property.
	private void Button2_Click(System.Object sender, System.EventArgs e)
	{
		LayoutControl1.Size = new System.Drawing.Size(100, 100);
	}

	// This method explicitly calls PerformLayout with no parameters, 
	// which raises the Layout event with the LayoutEventArgs properties
	// equal to Nothing.
	private void Button3_Click(System.Object sender, System.EventArgs e)
	{
		LayoutControl1.PerformLayout();
	}

}


// This custom control has the Layout event implented so that when 
// PerformLayout(AffectedControl, AffectedProperty) is called 
// on the control, where AffectedProperty equals "Bounds" the 
// textbox is centered on the control.
public class LayoutControl:
	System.Windows.Forms.UserControl
{
	internal System.Windows.Forms.TextBox TextBox1;

	public LayoutControl() : base()
	{        
		InitializeComponent();
	}

	private void InitializeComponent()
	{
		this.TextBox1 = new System.Windows.Forms.TextBox();
		this.SuspendLayout();
		this.TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
		this.TextBox1.Name = "TextBox1";
		this.TextBox1.TabIndex = 0;
		this.BackColor = System.Drawing.SystemColors.ControlDark;
		this.Controls.Add(this.TextBox1);
		this.Name = "LayoutControl";
		this.ResumeLayout(false);
		this.Layout += new LayoutEventHandler(LayoutControl_Layout);

	}

	// This method is called when the Layout event is fired. 
	// This happens by during the initial load, by calling PerformLayout
	// or by resizing, adding or removing controls or other actions that 
	// affect how the control is laid out. This method checks the 
	// value of e.AffectedProperty and changes the look of the 
	// control accordingly. 
	private void LayoutControl_Layout(object sender, 
		System.Windows.Forms.LayoutEventArgs e)
	{
		if (e.AffectedProperty != null)
		{
			if (e.AffectedProperty.Equals("Bounds"))
			{
				TextBox1.Left = (this.Width-TextBox1.Width)/2;
				TextBox1.Top = (this.Height-TextBox1.Height)/2;
			}
		}
		else
		{
			this.Size = new System.Drawing.Size(150, 160);
			TextBox1.Location = new System.Drawing.Point(16, 24);
		}
		TextBox1.Text = "Left = "+TextBox1.Left+" Top = "+TextBox1.Top;
	}

}



import System.Windows.Forms.*;
import System.Drawing.*;
public class LayoutForm extends System.Windows.Forms.Form
{
    public LayoutForm()
    {
        InitializeComponent();
    } //LayoutForm

    System.Windows.Forms.Button button1;
    System.Windows.Forms.Button button2;
    LayoutControl layoutControl1;
    System.Windows.Forms.Button button3;

    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.button3 = new System.Windows.Forms.Button();
        this.layoutControl1 = new LayoutControl();
        this.SuspendLayout();
        this.button1.set_Location(new System.Drawing.Point(16, 16));
        this.button1.set_Name("Button1");
        this.button1.set_Size(new System.Drawing.Size(120, 32));
        this.button1.set_TabIndex(0);
        this.button1.set_Text("Center textbox on control");
        this.button2.set_Location(new System.Drawing.Point(152, 16));
        this.button2.set_Name("Button2");
        this.button2.set_Size(new System.Drawing.Size(104, 32));
        this.button2.set_TabIndex(3);
        this.button2.set_Text("Shrink user control");
        this.button3.set_Location(new System.Drawing.Point(96, 232));
        this.button3.set_Name("Button3");
        this.button3.set_TabIndex(5);
        this.button3.set_Text("Reset");
        this.layoutControl1.set_BackColor(System.Drawing.SystemColors.
            get_ControlDark());
        this.layoutControl1.set_Location(new System.Drawing.Point(72, 64));
        this.layoutControl1.set_Name("LayoutControl1");
        this.layoutControl1.set_Size(new System.Drawing.Size(150, 160));
        this.layoutControl1.set_TabIndex(6);
        this.set_ClientSize(new System.Drawing.Size(292, 266));
        this.get_Controls().Add(this.button3);
        this.get_Controls().Add(this.button2);
        this.get_Controls().Add(this.button1);
        this.get_Controls().Add(this.layoutControl1);
        this.set_Name("Form1");
        this.set_Text("Form1");
        this.ResumeLayout(false);
        this.button1.add_Click(new System.EventHandler(button1_Click));
        this.button2.add_Click(new System.EventHandler(button2_Click));
        this.button3.add_Click(new System.EventHandler(button3_Click));
    } //InitializeComponent

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

    // This method explicitly calls raises the layout event on 
    // LayoutControl1, changing the Bounds property.
    private void button1_Click(Object sender, System.EventArgs e)
    {
        layoutControl1.PerformLayout(layoutControl1, "Bounds");
    } //button1_Click

    // This resize of LayoutControl1 implicitly triggers the layout event. 
    //  Changing the size of the control affects its Bounds property.
    private void button2_Click(Object sender, System.EventArgs e)
    {
        layoutControl1.set_Size(new System.Drawing.Size(100, 100));
    } //button2_Click

    // This method explicitly calls PerformLayout with no parameters, 
    // which raises the Layout event with the LayoutEventArgs properties
    // equal to Nothing.
    private void button3_Click(Object sender, System.EventArgs e)
    {
        layoutControl1.PerformLayout();
    } //button3_Click
} //LayoutForm

// This custom control has the Layout event implented so that when 
// PerformLayout(AffectedControl, AffectedProperty) is called 
// on the control, where AffectedProperty equals "Bounds" the 
// textbox is centered on the control.
public class LayoutControl extends System.Windows.Forms.UserControl
{
    System.Windows.Forms.TextBox textBox1;

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

    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        this.textBox1.set_BorderStyle(System.Windows.Forms.
            BorderStyle.FixedSingle);
        this.textBox1.set_Name("TextBox1");
        this.textBox1.set_TabIndex(0);
        this.set_BackColor(System.Drawing.SystemColors.get_ControlDark());
        this.get_Controls().Add(this.textBox1);
        this.set_Name("LayoutControl");
        this.ResumeLayout(false);
        this.add_Layout(new LayoutEventHandler(LayoutControl_Layout));
    } //InitializeComponent

    // This method is called when the Layout event is fired. 
    // This happens by during the initial load, by calling PerformLayout
    // or by resizing, adding or removing controls or other actions that 
    // affect how the control is laid out. This method checks the 
    // value of e.AffectedProperty and changes the look of the 
    // control accordingly. 
    private void LayoutControl_Layout(Object sender, 
        System.Windows.Forms.LayoutEventArgs e)
    {
        if (e.get_AffectedProperty() != null) {
            if (e.get_AffectedProperty().Equals("Bounds")) {
                textBox1.set_Left((this.get_Width() 
                    - textBox1.get_Width()) / 2);
                textBox1.set_Top((this.get_Height() 
                    - textBox1.get_Height()) / 2);
            }
        }
        else {
            this.set_Size(new System.Drawing.Size(150, 160));
            textBox1.set_Location(new System.Drawing.Point(16, 24));
        }
        textBox1.set_Text("Left = " + textBox1.get_Left() + " Top = " 
            + textBox1.get_Top());
    } //LayoutControl_Layout
} //LayoutControl 


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

.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

.NET Framework

Unterstützt in: 3.5, 3.0, 2.0, 1.1, 1.0

Community-Beiträge

HINZUFÜGEN
Anzeigen:
© 2014 Microsoft