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

LayoutEventHandler Delegate

Represents the method that will handle the Layout event of a Control.

[Visual Basic]
<Serializable>
Public Delegate Sub LayoutEventHandler( _
   ByVal sender As Object, _
   ByVal e As LayoutEventArgs _
)
[C#]
[Serializable]
public delegate void LayoutEventHandler(
   object sender,
   LayoutEventArgs e
);
[C++]
[Serializable]
public __gc __delegate void LayoutEventHandler(
   Object* sender,
   LayoutEventArgs* e
);

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your event handler must have the same parameters as the LayoutEventHandler delegate declaration.

sender
The source of the event.
e
A LayoutEventArgs that contains the event data.

Remarks

When you create a LayoutEventArgs delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Events and Delegates.

Example

[Visual Basic] 
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents textBox1 As System.Windows.Forms.TextBox
    Private label1 As System.Windows.Forms.Label
    Private layoutButton As System.Windows.Forms.Button
    Private components As System.ComponentModel.Container = Nothing


    Public Sub New()
        InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private Sub InitializeComponent()
        Me.layoutButton = New System.Windows.Forms.Button()
        Me.textBox1 = New System.Windows.Forms.TextBox()
        Me.label1 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        ' 
        ' layoutButton
        ' 
        Me.layoutButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom
        Me.layoutButton.Location = New System.Drawing.Point(72, 88)
        Me.layoutButton.Name = "layoutButton"
        Me.layoutButton.Size = New System.Drawing.Size(150, 23)
        Me.layoutButton.TabIndex = 0
        Me.layoutButton.Text = "Hello"
        ' 
        ' textBox1
        ' 
        Me.textBox1.Anchor = System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right
        Me.textBox1.Location = New System.Drawing.Point(24, 40)
        Me.textBox1.Name = "textBox1"
        Me.textBox1.Size = New System.Drawing.Size(248, 20)
        Me.textBox1.TabIndex = 1
        Me.textBox1.Text = "Hello"
        ' 
        ' label1
        ' 
        Me.label1.Location = New System.Drawing.Point(24, 16)
        Me.label1.Name = "label1"
        Me.label1.TabIndex = 2
        Me.label1.Text = "Button's Text:"
        ' 
        ' Form1
        ' 
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 129)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.label1, Me.textBox1, Me.layoutButton})
        Me.Name = "Form1"
        Me.Text = "Layout Sample"
        Me.ResumeLayout(False)
    End Sub

    ' This method will enforce that the form's width is the preferred size of 300 pixels,
    ' or, the size of the button + 50 pixels, whichever is smaller.
    Private Sub Form1_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles MyBase.Layout
        ' This event is raised once at startup with the AffectedControl
        ' and AffectedProperty proeprties on the LayoutEventArgs as null.
        ' The if statement protects against that case.
        If Not (e.AffectedControl Is Nothing) And Not (e.AffectedProperty Is Nothing) Then
            ' Make sure the affected property is the Bounds property
            ' of the form.
            If e.AffectedProperty.ToString() = "Bounds" Then
                ' If layoutButton's width + a 50 pixel padding is greater than the preferred 
                ' size of 300 pixels, then grow the form's width.
                If Me.layoutButton.Width + 50 > 300 Then
                    Me.Width = Me.layoutButton.Width + 50
                    ' If not, keep the form's width at 300 pixels.
                Else
                    Me.Width = 300
                End If

                ' Center layoutButton on the form.
                Me.layoutButton.Left = (Me.ClientSize.Width - Me.layoutButton.Width) / 2
            End If
        End If
    End Sub

    ' This method sets the Text property of layoutButton to the Text property
    ' of textBox1.  If the new text + a 20 pixel padding is larger than 
    ' the 150 pixel preferred size, adjust layoutButton's Width property.
    Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBox1.TextChanged
        ' Set the Text property of layoutButton.
        Me.layoutButton.Text = Me.textBox1.Text
        ' Get the width of the text using the proper font.
        Dim textWidth As Integer = CInt(Me.CreateGraphics().MeasureString(layoutButton.Text, layoutButton.Font).Width)

        ' If the width of the text + a 20 pixel padding is greater than the preferred size of
        ' 150 pixels, grow layoutButton's width.
        If textWidth + 20 > 150 Then
            ' Setting the size property on any control raises 
            ' the Layout event for it's container.
            Me.layoutButton.Width = textWidth + 20
            ' If not, keep layoutButton's width at 150 pixels.
        Else
            Me.layoutButton.Width = 150
        End If
    End Sub
End Class

[C#] 
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.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      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 will enforce that the form's width is the preferred size of 300 pixels,
   // or, the size of the button + 50 pixels, whichever is smaller.
   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 if statement protects against that case.
      if ((e.AffectedControl != null) && (e.AffectedProperty != null))
      {
         // Make sure the affected property is the Bounds property
         // of the form.
         if (e.AffectedProperty.ToString() == "Bounds") 
         {
            // If layoutButton's width + a 50 pixel padding is greater than the preferred 
            // size of 300 pixels, then grow 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 + a 20 pixel padding is larger than 
   // the 150 pixel preferred size, adjust 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 + a 20 pixel padding is greater than the preferred size of
      // 150 pixels, grow layoutButton's width.
      if ((textWidth + 20) > 150)
      {
         // Setting the size property on any control raises 
         // the Layout event for it's container.
         this.layoutButton.Width = textWidth + 20;
      }
         // If not, keep layoutButton's width at 150 pixels.
      else 
      {
         this.layoutButton.Width = 150;
      }
   }
}

[C++] 
public __gc class Form1 : public System::Windows::Forms::Form
{
private:
   System::Windows::Forms::TextBox* textBox1;
   System::Windows::Forms::Label* label1;
   System::Windows::Forms::Button* layoutButton;
   System::ComponentModel::Container* components;

public:
   Form1()
   {
      InitializeComponent();
   }

protected:
   void Dispose( bool disposing )
   {
      if( disposing )
      {
         if (components != 0) 
         {
            components->Dispose();
         }
      }
      Form::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 = System::Drawing::Point(72, 88);
      this->layoutButton->Name = S"layoutButton";
      this->layoutButton->Size = System::Drawing::Size(150, 23);
      this->layoutButton->TabIndex = 0;
      this->layoutButton->Text = S"Hello";
      // 
      // textBox1
      // 
      this->textBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>
         (System::Windows::Forms::AnchorStyles::Top
         | System::Windows::Forms::AnchorStyles::Left
         | System::Windows::Forms::AnchorStyles::Right);
      this->textBox1->Location = System::Drawing::Point(24, 40);
      this->textBox1->Name = S"textBox1";
      this->textBox1->Size = System::Drawing::Size(248, 20);
      this->textBox1->TabIndex = 1;
      this->textBox1->Text = S"Hello";
      this->textBox1->TextChanged += new System::EventHandler(this, &Form1::textBox1_TextChanged);
      // 
      // label1
      // 
      this->label1->Location = System::Drawing::Point(24, 16);
      this->label1->Name = S"label1";
      this->label1->TabIndex = 2;
      this->label1->Text = S"Button's Text:";
      // 
      // Form1
      // 
      this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
      this->ClientSize = System::Drawing::Size(292, 129);

      System::Windows::Forms::Control* temp0 [] = {this->label1, this->textBox1, this->layoutButton};

      this->Controls->AddRange(temp0);
      this->Name = S"Form1";
      this->Text = S"Layout Sample";
      this->Layout += new System::Windows::Forms::LayoutEventHandler(this, &Form1::Form1_Layout);
      this->ResumeLayout(false);

   }

   // This method will enforce that the form's width is the preferred size of 300 pixels,
   // or, the size of the button + 50 pixels, whichever is smaller.
   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 if statement protects against that case.
      if ((e->AffectedControl != 0) && (e->AffectedProperty != 0))
      {
         // Make sure the affected property is the Bounds property
         // of the form.
         if (e->AffectedProperty->ToString()->Equals(S"Bounds")) 
         {
            // If layoutButton's width + a 50 pixel padding is greater than the preferred 
            // size of 300 pixels, then grow 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 + a 20 pixel padding is larger than 
   // the 150 pixel preferred size, adjust layoutButton's Width property.
   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 + a 20 pixel padding is greater than the preferred size of
      // 150 pixels, grow 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;
      }
   }
};

[STAThread]
int main() 
{
   Application::Run(new Form1());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

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

See Also

System.Windows.Forms Namespace | LayoutEventArgs | Layout

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.