Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Control.PerformLayout Method (Control, String)

Forces the control to apply layout logic to all its child controls.

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

'Declaration
Public Sub PerformLayout ( _
	affectedControl As Control, _
	affectedProperty As String _
)
'Usage
Dim instance As Control
Dim affectedControl As Control
Dim affectedProperty As String

instance.PerformLayout(affectedControl, affectedProperty)
public void PerformLayout (
	Control affectedControl, 
	String affectedProperty
)
public function PerformLayout (
	affectedControl : Control, 
	affectedProperty : String
)

Parameters

affectedControl

A Control that represents the most recently changed control.

affectedProperty

The name of the most recently changed property on the control.

If the SuspendLayout method was called before calling the PerformLayout method, the Layout event is suppressed.

The affectedControl and affectedProperty parameters can both be set to a null reference (Nothing in Visual Basic). Doing so causes the AffectedControl and AffectedProperty properties of the LayoutEventArgs created to be set to a null reference (Nothing in Visual Basic).

The following code example demonstrates how to use the PerformLayout method. It also demonstrates ways in which the Layout event is raised. In this example, the Click event handler for Button1 explicitly calls PerformLayout. The Click event handler for Button2 implicitly calls PerformLayout. PerformLayout is also called when the form is loaded. Button3 returns the control to the state it was in when loaded. In each case, the Layout event is raised.

This is a complete example. To run the example, paste the following code in a blank form.


Imports System.Windows.Forms
Imports System.Drawing



Public Class LayoutForm
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents LayoutControl1 As LayoutControl
    Friend WithEvents Button3 As System.Windows.Forms.Button

    Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.LayoutControl1 = New LayoutControl
        Me.SuspendLayout()
        Me.Button1.Location = New System.Drawing.Point(16, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(120, 32)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Center textbox on control"
        Me.Button2.Location = New System.Drawing.Point(152, 16)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(104, 32)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Shrink user control"
        Me.Button3.Location = New System.Drawing.Point(96, 232)
        Me.Button3.Name = "Button3"
        Me.Button3.TabIndex = 5
        Me.Button3.Text = "Reset"
        Me.LayoutControl1.BackColor = System.Drawing.SystemColors.ControlDark
        Me.LayoutControl1.Location = New System.Drawing.Point(72, 64)
        Me.LayoutControl1.Name = "LayoutControl1"
        Me.LayoutControl1.Size = New System.Drawing.Size(150, 160)
        Me.LayoutControl1.TabIndex = 6
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.LayoutControl1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

    <System.STAThread()> Shared Sub Main()
        Application.Run(New LayoutForm)
    End Sub


   
    ' This method explicitly calls raises the layout event on 
    ' LayoutControl1, changing the Bounds property.
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        LayoutControl1.PerformLayout(LayoutControl1, "Bounds")
    End Sub

    ' This resize of LayoutControl1 implicitly triggers the layout event. 
    '   Changing the size of the control affects its Bounds property.
    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
        LayoutControl1.Size = New System.Drawing.Size(100, 100)
    End Sub

    ' This method explicitly calls PerformLayout with no parameters, 
    ' which raises the Layout event with the LayoutEventArgs properties
    ' equal to Nothing.
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        LayoutControl1.PerformLayout()
    End Sub

End Class


'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
    Inherits System.Windows.Forms.UserControl
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        Me.TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 0
        Me.BackColor = System.Drawing.SystemColors.ControlDark
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "LayoutControl"
        Me.ResumeLayout(False)

    End Sub

    '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 Sub LayoutControl_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles MyBase.Layout
        If Not e.AffectedProperty Is Nothing Then
            If e.AffectedProperty.Equals("Bounds") Then
                TextBox1.Left = (Me.Width - TextBox1.Width) / 2
                TextBox1.Top = (Me.Height - TextBox1.Height) / 2
            End If
        Else
            Me.Size = New System.Drawing.Size(150, 160)
            TextBox1.Location = New System.Drawing.Point(16, 24)
        End If
        TextBox1.Text = "Left = " & TextBox1.Left & " Top = " & TextBox1.Top
    End Sub

End Class


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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.