Layout Events in Windows Forms LayoutManagers

28 Apr 20213 minutes to read

The list of events and a detailed explanation about each of them is given in the following sections.

Layout events Description
ContainerControlChanged This event is triggered when the ContainerControl property is changed.
ProvideLayoutInformation This event is triggered to obtain the preferred size information for a Child control during layout.

ContainerControlChanged event

This event is handled, when the ContainerControl property is changed.

The event handler receives an argument of type EventArgs containing data related to this event.

// Initialize the new ContainerControl.

this.borderLayout1.ContainerControl = this.panel1;



// Handle the ContainerControlChanged event.

this.borderLayout1.ContainerControlChanged+=new EventHandler(borderLayout1_ContainerControlChanged);



private void borderLayout1_ContainerControlChanged(object sender, EventArgs e)

{

// ContainerControlChanged event is raised when the ContainerControl for the Layout Manager is changed. The below statement can be seen in the output window at runtime.

Console.Write("Container Control is changed to Panel" );

}
' Initialize the new ContainerControl. 

Me.borderLayout1.ContainerControl = Me.panel1 



' Handle the ContainerControlChanged event. 

AddHandler Me.borderLayout1.ContainerControlChanged, AddressOf borderLayout1_ContainerControlChanged 



Private Sub borderLayout1_ContainerControlChanged(ByVal sender As Object, ByVal e As EventArgs)

    ' ContainerControlChanged event is raised when the ContainerControl for the Layout Manager is changed. The below statement can be seen in the output window at runtime. 

    Console.Write("Container Control is changed to Panel")

End Sub

ProvideLayoutInformation event

This event is triggered to obtain the preferred size information for a Child control during layout.

The event handler receives an argument of type ProvideLayoutInformationEventArgs containing data related to this event. The ProvideLayoutInformationEventArgs members provide information specific to this event.

Members Description
Control Specifies whether the child controls should be automatically aligned.
Handle Specifies whether this event was handled and a value provided.
Requested Returns the type of information requested.
Size Gets/sets the size to be returned.

You can handle this event to auto size the Label control when you increase/decrease the form width.

private void flowLayout1_ProvideLayoutInformation(object sender, Syncfusion.Windows.Forms.Tools.ProvideLayoutInformationEventArgs e)

{

if (e.Control == this.label1 && e.Requested == LayoutInformationType.PreferredSize)

{

Graphics g = this.CreateGraphics();

SizeF measure = g.MeasureString(this.label1.Text, this.label1.Font, this.ClientRectangle.Width);

e.Size = new Size(this.ClientRectangle.Width-20, (int)measure.Height + 5);

e.Handled = true;

g.Dispose();

}

}
Private Sub flowLayout1_ProvideLayoutInformation(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Tools.ProvideLayoutInformationEventArgs)

If e.Control = Me.label1 AndAlso e.Requested = LayoutInformationType.PreferredSize Then

Dim g As Graphics = Me.CreateGraphics()

Dim measure As SizeF = g.MeasureString(Me.label1.Text, Me.label1.Font, Me.ClientRectangle.Width)

e.Size = New Size(Me.ClientRectangle.Width - 20, CInt(measure.Height) + 5)

e.Handled = True

g.Dispose()

End If

End Sub