Creating Layout Strategies

Arranging input controls and their associated fields is not done within the AutoFormPanel control - it's the job of a layout strategy class. Layout strategies must implement the ILayoutStrategy interface in order for AutoFormPanel to use them. The best way to see how they work is to take a look at the two included classes, GridLayoutStrategy and StackedLayoutStrategy, but here are the basic steps required to create a new layout class of your own:

1. Inherit from ILayoutStrategy:
    public class NewLayoutStrategy: ILayoutStrategy {


2. Add some private fields. The layout strategy will need to store two dictionaries, containing the input controls and field controls it needs to arrange:
        private Dictionary<BaseFieldDefinition, Control> _fieldControls;
        private Dictionary<BaseFieldDefinition, Control> _fieldLabels;

3. Define the FormPanel property. This holds a reference to the AutoFormPanel whose controls will be arranged:
        public AutoFormPanel FormPanel { get; set; }

4. Implement the Initialise() method.
        void Initialise(Dictionary<BaseFieldDefinition,Control> inputFields,
            Dictionary<BaseFieldDefinition,Control> fieldLabels) {
            _fieldControls = inputFields;
            _fieldLabels = fieldLabels;
This method is called before the layout strategy will be asked to arrange the input controls of the form. At this point the two dictionaries we declared in step 2 should be set to the two dictionaries passed to the method.

5. Implement CreateLayout().
        public void CreateLayout()
            // Your code here. It should first remove all controls
            // from the AutoFormPanel control, in case it is 
            // being called multiple times. Then loop through
            // the Keys property of the _fieldControls variable 
            // and add the input control and field label for each one.
This method actually does the work of arranging input controls on the AutoFormPanel control. How it does this is entirely up to you, and depends on the requirements of the layout you want to acheive. For example, the two classes provided with AutoForm use Grid controls, with container controls embedded in a DockPanel in each cell.

6. Implement DropLayout(). This method is called when the AutoForm control has been unloaded (in particular from a TabControl whose selected tab has changed). The DropLayout method should drop any references to all input controls it currently maintains - i.e. the two dictionary properties it received in the Initialise() method. See the included GridLayoutStrategy and StackedLayoutsStrategy classes for examples.

7. Implement LayoutChanged(). This method is currently unused, but is included in the ILayoutStrategy interface for use later, when it is planned to make AutoForms form generation fully dynamic. The method can be empty.

Last edited Oct 24, 2014 at 12:26 PM by DevKnightlie, version 5