public static void CheckLabelWidthsOnForm(Control parentControl)
{
foreach (Control child in parentControl.Controls)
{
CheckLabelWidthsOnForm(child);
if (child is Label)
{
if (child.Right > parentControl.ClientRectangle.Right)
{
//System.Diagnostics.Trace.WriteLine("Control: " + child.ToString() + " X: " + child.Left + " ParentRight: " + parentControl.ClientRectangle.Right);
//System.Diagnostics.Trace.WriteLine("MaxSize was: " + child.MaximumSize.ToString() + "; size was: " + child.Size.ToString());
if (child.MaximumSize.Width > 0)
{
child.MaximumSize = new Size(parentControl.ClientRectangle.Right - child.Left - 10, 0);
}
else
{
child.Size = new Size(parentControl.ClientRectangle.Right - child.Left - 10, child.Height);
}
//System.Diagnostics.Trace.WriteLine("MaxSize now: " + child.MaximumSize.ToString() + "; size now: " + child.Size.ToString());
}
}
}
}