Re-positioning controls on a form The following piece of code is used to re-position controls on a form considering different options.

Once you have multiple controls this becomes quite messy. I would appreciate your improvement suggestions.

private void dlgOptionsForm_Load(object sender, System.EventArgs e)
    {

        if (_hideAccountingOption)
        {
            grpAccountingOption.Visible = false;
            Height -= grpAccountingOption.Height;

            grpPeriod.Top -= (grpAccountingOption.Height + 8);
            grpPrintOptions.Top -= (grpAccountingOption.Height + 8);
            grpInvoiceInformation.Top -= (grpAccountingOption.Height + 8);
            grpHND.Top -= (grpAccountingOption.Height + 8);
            chkPrint.Checked = false;
            grpArchiveDocument.Top -= (grpAccountingOption.Height + 8);
            grpBalanceControlList.Top -= (grpAccountingOption.Height + 8);
        }

        if (_hidePeriod)
        {
            grpPeriod.Visible = false;
            Height -= grpPeriod.Height;

            grpPrintOptions.Top -= (grpPeriod.Height + 8);
            grpInvoiceInformation.Top -= (grpPeriod.Height + 8);
            grpHND.Top -= (grpPeriod.Height + 8);
            chkPrint.Checked = false;
            grpArchiveDocument.Top -= (grpPeriod.Height + 8);
            grpBalanceControlList.Top -= (grpPeriod.Height + 8);
        }

        if (_hidePrintOptions)
        {
            grpPrintOptions.Visible = false;
            this.Height -= grpPrintOptions.Height;
            grpInvoiceInformation.Top -= (grpPrintOptions.Height + 8);
            grpHND.Top -= (grpPrintOptions.Height + 8);
            chkPrint.Checked = false;
            grpArchiveDocument.Top -= (grpPrintOptions.Height + 8);
            grpBalanceControlList.Top -= (grpPrintOptions.Height + 8);
        }

        ....
    }
Solution 1:

If all of your grp* inherits of the same object (say Control), what you can do is:

private List&lt;Control&gt; _groups = new List&lt;Control&gt;();

.ctor()
{
   // Populate _groups
   _groups.Add(grpPeriod);
   _groups.Add(grpPrintOptions);
   _groups.Add(grpInvoiceInformation);
   _groups.Add(grpHND);
   _groups.Add(grpArchiveDocument);
   _groups.Add(grpBalanceControlList);
}


Now you method becomes:

private void dlgOptionsForm_Load(object sender, System.EventArgs e)
{
    if (_hideAccountingOption)
    {      
        Adjust(grpAccountingOption);
    }
    if (_hidePeriod)
    {
        Adjust(grpPeriod);
    }

    if (_hidePrintOptions)
    {
        Adjust(grpPrintOptions);
    }

    ....
}

private void Adjust(Control control)
{
    control.Visible = false;
    this.Height -= control.Height;
    chkPrint.Checked = false;

    foreach (var item in _groups)
    {
        if(control != item)
            item.Top -= (control.Height + 8);
    }
}




EDIT

Solution 2:

Dictionary&lt;Func&lt;bool&gt;, Control&gt; _groups= new Dictionary&lt;Func&lt;bool&gt;, Control&gt;();

.ctor()
{
   // Populate _groups
   _groups.Add(() =&gt; _hidePeriod, grpPeriod);
   _groups.Add(() =&gt; _hideInvoiceInformation, grpInvoiceInformation);
   _groups.Add(() =&gt; /* your bool variable */, /* the associated control */);
   // ...
}

private void dlgOptionsForm_Load(object sender, System.EventArgs e)
{
    foreach (var item in _groups)
    {
        if (item.Key()) // Evaluate the boolean variable
            Adjust(item.Value);
    }
}

private void Adjust(Control control)
{
    control.Visible = false;
    this.Height -= control.Height;
    chkPrint.Checked = false;

    foreach (var item in _groups)
    {
        if(control != item)
            item.Top -= (control.Height + 8);
    }
}