/// <summary>write the code for the designer file where the properties of the control are written</summary>
        public virtual ProcessTemplate SetControlProperties(TFormWriter writer, TControlDef ctrl)
        {
            bool AutomDataValidation;
            string ReasonForAutomValidation;
            bool IsDetailNotMaster;

            writer.SetControlProperty(ctrl, "Name", "\"" + ctrl.controlName + "\"");

            if (FLocation && !ctrl.HasAttribute("Dock"))
            {
                writer.SetControlProperty(ctrl, "Location", "new System.Drawing.Point(2,2)");
            }

            #region Aligning and stretching

            if (ctrl.HasAttribute("Align")
                && !(ctrl.HasAttribute("Stretch")))
            {
                if ((ctrl.GetAttribute("Align").ToLower() == "right")
                    || (ctrl.GetAttribute("Align").ToLower() == "top-right"))
                {
                    writer.SetControlProperty(ctrl,
                        "Anchor",
                        "((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)))");
                }
                else if (ctrl.GetAttribute("Align").ToLower() == "middle-right")
                {
                    writer.SetControlProperty(ctrl,
                        "Anchor",
                        "((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Right))");
                }
                else if (ctrl.GetAttribute("Align").ToLower() == "bottom-right")
                {
                    writer.SetControlProperty(ctrl,
                        "Anchor",
                        "((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)))");
                }
                else if ((ctrl.GetAttribute("Align").ToLower() == "center")
                         || (ctrl.GetAttribute("Align").ToLower() == "top-center"))
                {
                    writer.SetControlProperty(ctrl,
                        "Anchor",
                        "((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top))");
                }
                else if (ctrl.GetAttribute("Align").ToLower() == "middle-center")
                {
                    writer.SetControlProperty(ctrl,
                        "Anchor",
                        "((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.None))");
                }
                else if (ctrl.GetAttribute("Align").ToLower() == "bottom-center")
                {
                    writer.SetControlProperty(ctrl,
                        "Anchor",
                        "((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom))");
                }
                else if ((ctrl.GetAttribute("Align").ToLower() == "bottom")
                         || (ctrl.GetAttribute("Align").ToLower() == "bottom-left"))
                {
                    writer.SetControlProperty(ctrl,
                        "Anchor",
                        "((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Bottom)))");
                }
                else if ((ctrl.GetAttribute("Align").ToLower() == "middle")
                         || (ctrl.GetAttribute("Align").ToLower() == "middle-left"))
                {
                    writer.SetControlProperty(ctrl,
                        "Anchor",
                        "((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Left))");
                }
                else if ((ctrl.GetAttribute("Align").ToLower() == "left")
                         || (ctrl.GetAttribute("Align").ToLower() == "top")
                         || (ctrl.GetAttribute("Align").ToLower() == "top-left"))
                {
                    // do nothing (here just to avoid throwing the following Exception)
                    Console.WriteLine(
                        "HINT: Attribute 'Align' with value 'left', 'top' or 'top-left' does not affect the layout since these create the default alignment. Control: '"
                        +
                        ctrl.controlName + "'.");
                }
                else
                {
                    throw new Exception("Invalid value for Attribute 'Align' of Control '" + ctrl.controlName + "': '" + ctrl.GetAttribute(
                            "Align") +
                        "'. Supported values are: Simple: left, right, center; top, middle, bottom; Combined: top-left, middle-left, bottom-left, top-center, middle-center, bottom-center, top-right, middle-right, bottom-right.");
                }
            }

            if (ctrl.HasAttribute("Stretch"))
            {
                if (ctrl.GetAttribute("Stretch").ToLower() == "horizontally")
                {
                    if ((!ctrl.HasAttribute("Align"))
                        || (ctrl.HasAttribute("Align") && (ctrl.GetAttribute("Align").ToLower() == "top")))
                    {
                        // Horizontally stretched, top aligned (=default)
                        writer.SetControlProperty(
                            ctrl,
                            "Anchor",
                            "((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)))");
                    }
                    else
                    {
                        if (ctrl.GetAttribute("Align").ToLower() == "bottom")
                        {
                            // Horizontally stretched, bottom aligned
                            writer.SetControlProperty(
                                ctrl,
                                "Anchor",
                                "((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)))");
                        }
                        else if (ctrl.GetAttribute("Align").ToLower() == "middle")
                        {
                            // Horizontally stretched, vertically centered
                            writer.SetControlProperty(
                                ctrl,
                                "Anchor",
                                "((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)))");
                        }
                        else
                        {
                            throw new Exception("Invalid value '" + ctrl.GetAttribute(
                                    "Align") + "' for Attribute 'Align' of Control '" + ctrl.controlName +
                                "' whose Attribute 'Stretch' is set to '" +
                                ctrl.GetAttribute("Stretch") +
                                "'. Supported values are: top, middle, bottom.");
                        }
                    }
                }
                else if (ctrl.GetAttribute("Stretch").ToLower() == "vertically")
                {
                    if ((!ctrl.HasAttribute("Align"))
                        || (ctrl.HasAttribute("Align") && (ctrl.GetAttribute("Align").ToLower() == "left")))
                    {
                        // Vertically stretched, left aligned (=default)
                        writer.SetControlProperty(
                            ctrl,
                            "Anchor",
                            "((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Bottom)))");
                    }
                    else
                    {
                        if (ctrl.GetAttribute("Align").ToLower() == "right")
                        {
                            // Vertically stretched, right aligned
                            writer.SetControlProperty(
                                ctrl,
                                "Anchor",
                                "((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right) | System.Windows.Forms.AnchorStyles.Bottom)))");
                        }
                        else if (ctrl.GetAttribute("Align").ToLower() == "center")
                        {
                            // Vertically stretched, horizontally centered
                            writer.SetControlProperty(
                                ctrl,
                                "Anchor",
                                "((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)))");
                        }
                        else
                        {
                            throw new Exception("Invalid value '" + ctrl.GetAttribute(
                                    "Align") + "' for Attribute 'Align' of Control '" + ctrl.controlName +
                                "' whose Attribute 'Stretch' is set to  '" +
                                ctrl.GetAttribute("Stretch") +
                                "'. Supported values are: left, center, right.");
                        }
                    }
                }
                else if (ctrl.GetAttribute("Stretch").ToLower() == "fully")
                {
                    // Fully stretched
                    writer.SetControlProperty(
                        ctrl,
                        "Anchor",
                        "((System.Windows.Forms.AnchorStyles)(((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right) | System.Windows.Forms.AnchorStyles.Bottom))))");

                    if (ctrl.HasAttribute("Align"))
                    {
                        Console.WriteLine(
                            "WARNING for Control '" + ctrl.controlName +
                            "': Attribute 'Align' gets ignored when Attribute 'Stretch' with value 'fully' is used.");
                    }
                }
                else if (ctrl.GetAttribute("Stretch").ToLower() == "none")
                {
                    // do nothing (here just to avoid throwing the following Exception)
                    Console.WriteLine(
                        "HINT: Attribute 'Stretch' with value 'none' does not affect the layout since this is the default. Control: '" +
                        ctrl.controlName + "'.");
                }
                else
                {
                    throw new Exception("Invalid value for Attribute 'Stretch' of Control '" + ctrl.controlName + "': '" +
                        ctrl.GetAttribute("Stretch") + "'. Supported values are: horizontally, vertically, fully, none.");
                }
            }

            #endregion

            if (ctrl.HasAttribute("Dock"))
            {
                writer.SetControlProperty(ctrl, "Dock");
            }

            if (ctrl.HasAttribute("Visible")
                && (ctrl.GetAttribute("Visible").ToLower() == "false"))
            {
                writer.SetControlProperty(ctrl, "Visible", "false");
            }

            if (ctrl.HasAttribute("Enabled")
                && (ctrl.GetAttribute("Enabled").ToLower() == "false"))
            {
                writer.SetControlProperty(ctrl, "Enabled", "false");
            }
            else if ((ctrl.GetAction() != null) && (TYml2Xml.GetAttribute(ctrl.GetAction().actionNode, "InitiallyEnabled").ToLower() == "false"))
            {
                string ActionEnabling = ctrl.controlName + ".Enabled = false;" + Environment.NewLine;
                writer.Template.AddToCodelet("INITACTIONSTATE", ActionEnabling);
            }

            if (ctrl.HasAttribute("TabStop")
                && (ctrl.GetAttribute("TabStop").ToLower() == "false"))
            {
                writer.SetControlProperty(ctrl, "TabStop", "false");
            }

            if (ctrl.HasAttribute("TabIndex"))
            {
                writer.SetControlProperty(ctrl, "TabIndex", ctrl.GetAttribute("TabIndex"));
            }

            if (ctrl.HasAttribute("BorderStyle"))
            {
                writer.SetControlProperty(ctrl, "BorderStyle", "System.Windows.Forms.BorderStyle." + ctrl.GetAttribute("BorderStyle"));

                if (ctrl.GetAttribute("BorderStyle").ToLower() == "none")
                {
                    writer.SetControlProperty(ctrl, "Margin", "new System.Windows.Forms.Padding(0, 5, 0, 0)");
                }
            }

            if (ctrl.HasAttribute("Padding"))
            {
                writer.SetControlProperty(ctrl, "Padding", "new System.Windows.Forms.Padding(" + ctrl.GetAttribute("Padding") + ")");
            }

            if (ctrl.HasAttribute("Margin"))
            {
                string margin = ctrl.GetAttribute("Margin");

                if (margin != "0")
                {
                    writer.SetControlProperty(ctrl, "Margin", "new System.Windows.Forms.Padding(" + margin + ")");
                }
            }

            if (ctrl.HasAttribute("BackColor"))
            {
                writer.SetControlProperty(ctrl, "BackColor", ctrl.GetAttribute("BackColor"));
            }

            if (ctrl.HasAttribute("AutoScroll"))
            {
                writer.SetControlProperty(ctrl, "AutoScroll", ctrl.GetAttribute("AutoScroll"));
            }

            // needed so that ctrl.Height and ctrl.Width return correct values
            ctrl.SetAttribute("DefaultWidth", FDefaultWidth.ToString());
            ctrl.SetAttribute("DefaultHeight", FDefaultHeight.ToString());

            if (ctrl.HasAttribute("Width") || ctrl.HasAttribute("Height"))
            {
                if (!ctrl.HasAttribute("Width"))
                {
                    ctrl.SetAttribute("Width", FDefaultWidth.ToString());
                }

                if (!ctrl.HasAttribute("Height"))
                {
                    ctrl.SetAttribute("Height", FDefaultHeight.ToString());
                }

                if (ctrl.HasAttribute("Width") && ctrl.HasAttribute("Height"))
                {
                    writer.SetControlProperty(ctrl, "Size", "new System.Drawing.Size(" +
                        ctrl.GetAttribute("Width").ToString() + ", " + ctrl.GetAttribute("Height").ToString() + ")");
                }
            }
            else if (ctrl.GetAttribute("Dock").ToLower() == "fill")
            {
                // no size information for Dock Fill
            }
            else
            {
                writer.SetControlProperty(ctrl, "Size",
                    "new System.Drawing.Size(" + FDefaultWidth.ToString() + ", " + FDefaultHeight.ToString() + ")");
            }

            if (ctrl.HasAttribute("SuppressChangeDetection") && (ctrl.GetAttribute("SuppressChangeDetection").ToLower() == "true"))
            {
                writer.SetControlProperty(ctrl, "Tag", "\"SuppressChangeDetection\"");
            }

            if (ctrl.GetAction() != null)
            {
                string ActionToPerform = ctrl.GetAction().actionName;

                // deal with enabling and disabling of action, affecting the menu item
                if (!writer.Template.FCodelets.Keys.Contains("ENABLEDEPENDINGACTIONS_" + ActionToPerform))
                {
                    string ActionEnabling = "";
                    ActionEnabling += "if (e.ActionName == \"" + ActionToPerform + "\")" + Environment.NewLine;
                    ActionEnabling += "{" + Environment.NewLine;
                    ActionEnabling += "    {#ENABLEDEPENDINGACTIONS_" + ActionToPerform + "}" + Environment.NewLine;
                    ActionEnabling += "}" + Environment.NewLine;
                    writer.Template.AddToCodelet("ACTIONENABLING", ActionEnabling);
                }

                AddToActionEnabledEvent(writer, ActionToPerform, ctrl.controlName);

                // deal with action handler
                if (((ActionToPerform == "actEditFilter") || (ActionToPerform == "actEditFindNext") || (ActionToPerform == "actEditFindPrevious")
                     || (ActionToPerform == "actEditFind")) && !writer.FCodeStorage.FControlList.ContainsKey("pnlFilterAndFind"))
                {
                    // Do nothing unless there is a manual code handler (on screens with user controls)
                    if (writer.FCodeStorage.ManualFileExistsAndContains("void MniFilterFind_Click("))
                    {
                        AssignEventHandlerToControl(writer, ctrl, "Click", "MniFilterFind_Click");
                    }
                    else if (ActionToPerform == "actEditFind")
                    {
                        TLogging.Log("No implementation of actEditFind on this screen");
                    }
                }
                else
                {
                    AssignEventHandlerToControl(writer, ctrl, "Click", ActionToPerform);
                }

                TActionHandler ActionHandler = writer.CodeStorage.FActionList[ActionToPerform];
                SetControlActionProperties(writer, ctrl, ActionHandler);

                string strMniFilterFindClick = "void MniFilterFind_Click(";

                if ((ActionToPerform == "actEditFind") && (ctrl.controlName == "mniEditFind")
                    && (writer.FCodeStorage.FControlList.ContainsKey("pnlFilterAndFind")
                        || writer.FCodeStorage.ManualFileExistsAndContains(strMniFilterFindClick)))
                {
                    writer.SetControlProperty("mniEditFind", "ShortcutKeys", "Keys.F | Keys.Control", false);
                }

                if ((ActionToPerform == "actEditFindNext") && (ctrl.controlName == "mniEditFindNext")
                    && (writer.FCodeStorage.FControlList.ContainsKey("pnlFilterAndFind")
                        || writer.FCodeStorage.ManualFileExistsAndContains(strMniFilterFindClick)))
                {
                    writer.SetControlProperty("mniEditFindNext", "ShortcutKeys", "Keys.F3", false);
                }

                if ((ActionToPerform == "actEditFindPrevious") && (ctrl.controlName == "mniEditFindPrevious")
                    && (writer.FCodeStorage.FControlList.ContainsKey("pnlFilterAndFind")
                        || writer.FCodeStorage.ManualFileExistsAndContains(strMniFilterFindClick)))
                {
                    writer.SetControlProperty("mniEditFindPrevious", "ShortcutKeys", "Keys.F3 | Keys.Shift", false);
                }

                if ((ActionToPerform == "actEditTop") && (ctrl.controlName == "mniEditTop"))
                {
                    writer.SetControlProperty("mniEditTop", "ShortcutKeys", "Keys.Home | Keys.Control", false);
                }

                if ((ActionToPerform == "actEditPrevious") && (ctrl.controlName == "mniEditPrevious"))
                {
                    writer.SetControlProperty("mniEditPrevious", "ShortcutKeys", "Keys.Up | Keys.Control", false);
                }

                if ((ActionToPerform == "actEditNext") && (ctrl.controlName == "mniEditNext"))
                {
                    writer.SetControlProperty("mniEditNext", "ShortcutKeys", "Keys.Down | Keys.Control", false);
                }

                if ((ActionToPerform == "actEditBottom") && (ctrl.controlName == "mniEditBottom"))
                {
                    writer.SetControlProperty("mniEditBottom", "ShortcutKeys", "Keys.End | Keys.Control", false);
                }

                if ((ActionToPerform == "actEditFocusGrid") && (ctrl.controlName == "mniEditFocusGrid"))
                {
                    writer.SetControlProperty("mniEditFocusGrid", "ShortcutKeys", "Keys.G | Keys.Control", false);
                }

                if ((ActionToPerform == "actEditFilter") && (ctrl.controlName == "mniEditFilter")
                    && (writer.FCodeStorage.FControlList.ContainsKey("pnlFilterAndFind")
                        || writer.FCodeStorage.ManualFileExistsAndContains(strMniFilterFindClick)))
                {
                    writer.SetControlProperty("mniEditFilter", "ShortcutKeys", "Keys.R | Keys.Control", false);
                }

                if ((ActionToPerform == "actSave") && (ctrl.controlName == "mniFileSave"))
                {
                    ProcessTemplate snipCtrlS = writer.FTemplate.GetSnippet("PROCESSCMDKEYCTRLS");
                    writer.FTemplate.InsertSnippet("PROCESSCMDKEY", snipCtrlS);
                    writer.SetControlProperty("mniFileSave", "ShortcutKeys", "Keys.S | Keys.Control", false);
                }

                if ((ActionToPerform == "actPrint") && (ctrl.controlName == "mniFilePrint"))
                {
                    writer.SetControlProperty("mniFilePrint", "ShortcutKeys", "Keys.P | Keys.Control", false);
                }

                if (FCodeStorage.ManualFileExistsAndContains(" " + ActionHandler.actionName.Substring(3) + "(Form AParentForm)"))
                {
                    writer.SetEventHandlerFunction(ActionHandler.actionName.Substring(3), "", ActionHandler.actionName.Substring(
                            3) + "(this);");
                }
            }
            else if (ctrl.HasAttribute("ActionClick"))
            {
                if (ctrl.GetAttribute("ActionClick").EndsWith("FilterFind_Click"))
                {
                    // MniFilterFind_Click is part of the base template for many forms.
                    // We only create an action handler if the screen has a pnlFilterAndFind
                    if (writer.FCodeStorage.FControlList.ContainsKey("pnlFilterAndFind"))
                    {
                        AssignEventHandlerToControl(writer, ctrl, "Click", ctrl.GetAttribute("ActionClick"));
                    }
                }
                else
                {
                    // The control is not mniEditFilter or mniEditFind, so just write the resource file item
                    // The manual code file will have the event handler itself
                    AssignEventHandlerToControl(writer, ctrl, "Click", ctrl.GetAttribute("ActionClick"));
                }
            }
            else if (ctrl.HasAttribute("ActionDoubleClick"))
            {
                AssignEventHandlerToControl(writer, ctrl, "DoubleClick", ctrl.GetAttribute("ActionDoubleClick"));
            }
            else if (ctrl.HasAttribute("ActionOpenScreen"))
            {
                AssignEventHandlerToControl(writer, ctrl, "Click", "OpenScreen" + ctrl.controlName.Substring(ctrl.controlTypePrefix.Length));
                string ActionHandler =
                    "/// auto generated" + Environment.NewLine +
                    "protected void OpenScreen" + ctrl.controlName.Substring(ctrl.controlTypePrefix.Length) + "(object sender, EventArgs e)" +
                    Environment.NewLine +
                    "{" + Environment.NewLine;

                string ActionOpenScreen = ctrl.GetAttribute("ActionOpenScreen");
                ActionHandler += "    " + ActionOpenScreen + " frm = new " + ActionOpenScreen +
                                 "(this);" + Environment.NewLine;

                if (ActionOpenScreen.Contains("."))
                {
                    string namespaceOfScreen = ActionOpenScreen.Substring(0, ActionOpenScreen.LastIndexOf("."));
                    writer.Template.AddToCodelet("USINGNAMESPACES", "using " + namespaceOfScreen + ";" + Environment.NewLine, false);
                }

                // Does PropertyForSubScreens fit a property in the new screen? eg LedgerNumber
                if (FCodeStorage.HasAttribute("PropertyForSubScreens"))
                {
                    string propertyName = FCodeStorage.GetAttribute("PropertyForSubScreens");

                    if (FCodeStorage.ImplementationContains(ctrl.GetAttribute("ActionOpenScreen"), " " + propertyName + Environment.NewLine))
                    {
                        ActionHandler += "    frm." + propertyName + " = F" + propertyName + ";" + Environment.NewLine;
                    }
                }

                /*                for (string propertyName in FCodeStorage.GetFittingProperties(ctrl.GetAttribute("ActionOpenScreen")))
                 *              {
                 *                  ActionHandler += "    frm." + propertyName + " = F" + propertyName + ";" + Environment.NewLine;
                 *              }
                 */
                ActionHandler += "    frm.Show();" + Environment.NewLine;
                ActionHandler += "}" + Environment.NewLine + Environment.NewLine;

                FCodeStorage.FActionHandlers += ActionHandler;
            }

            if (ctrl.HasAttribute("Enabled"))
            {
                AddToActionEnabledEvent(writer, ctrl.GetAttribute("Enabled"), ctrl.controlName);
            }

            if (ctrl.HasAttribute("OnChange"))
            {
                AssignEventHandlerToControl(writer, ctrl,
                    GetEventNameForChangeEvent(),
                    GetEventHandlerTypeForChangeEvent(),
                    ctrl.GetAttribute("OnChange"));
            }

            if (ctrl.HasAttribute("OnEnter"))
            {
                AssignEventHandlerToControl(writer, ctrl,
                    "Enter",
                    "System.EventHandler",
                    ctrl.GetAttribute("OnEnter"));
            }

            if (ctrl.HasAttribute("OnLeave"))
            {
                AssignEventHandlerToControl(writer, ctrl,
                    "Leave",
                    "System.EventHandler",
                    ctrl.GetAttribute("OnLeave"));
            }

            if (ctrl.HasAttribute("Tooltip"))
            {
                writer.Template.AddToCodelet("INITUSERCONTROLS", "FPetraUtilsObject.SetStatusBarText(" + ctrl.controlName +
                    ", Catalog.GetString(\"" +
                    ctrl.GetAttribute("Tooltip") +
                    "\"));" + Environment.NewLine);
            }
            else if (ctrl.controlName == "grdDetails")
            {
                writer.Template.AddToCodelet("INITUSERCONTROLS", "FPetraUtilsObject.SetStatusBarText(" + ctrl.controlName +
                    ", Catalog.GetString(\"Use the mouse or navigation keys to select a data row to view or edit\"));" + Environment.NewLine);
            }
            else if (ctrl.controlName == "btnNew")
            {
                writer.Template.AddToCodelet("INITUSERCONTROLS", "FPetraUtilsObject.SetStatusBarText(" + ctrl.controlName +
                    ", Catalog.GetString(\"Click to create a new record\"));" + Environment.NewLine);
            }
            else if (ctrl.controlName == "btnDelete")
            {
                writer.Template.AddToCodelet("INITUSERCONTROLS", "FPetraUtilsObject.SetStatusBarText(" + ctrl.controlName +
                    ", Catalog.GetString(\"Click to delete the highlighted record(s)\"));" + Environment.NewLine);
            }
            else if (ctrl.controlName.StartsWith("spt"))
            {
                writer.Template.AddToCodelet(
                    "INITUSERCONTROLS",
                    "FPetraUtilsObject.SetStatusBarText(" + ctrl.controlName +
                    ", Catalog.GetString(\"Use the arrow keys to change the Splitter position and change the relative proportions of the panels\"));"
                    +
                    Environment.NewLine);
            }

            //TODO: CT
//            if (ctrl.HasAttribute("DefaultValue"))
//            {
//                writer.Template.AddToCodelet("DEFAULTOVERRIDE", UndoValue(ctrl, ;
//            }

//Console.WriteLine("ctrl.controlTypePrefix: " + ctrl.controlName + ": " + ctrl.controlTypePrefix);
            if (ctrl.HasAttribute("PartnerShortNameLookup"))
            {
                LinkControlPartnerShortNameLookup(writer, ctrl);
            }
            else if (ctrl.HasAttribute("DataField"))
            {
                string dataField = ctrl.GetAttribute("DataField");

                TTableField field = TDataBinding.GetTableField(ctrl, dataField, out IsDetailNotMaster, true);

                LinkControlDataField(writer, ctrl, field, IsDetailNotMaster);
                DataFieldUndoCapability(writer, ctrl, field, IsDetailNotMaster);
            }
            else if (writer.CodeStorage.HasAttribute("MasterTable") || writer.CodeStorage.HasAttribute("DetailTable"))
            {
                //if (ctrl.controlTypePrefix != "lbl" && ctrl.controlTypePrefix != "pnl" && ctrl.controlTypePrefix != "grp" &&
                if (!((this is LabelGenerator) || (this is LinkLabelGenerator)))
                {
                    TTableField field = TDataBinding.GetTableField(ctrl, ctrl.controlName.Substring(
                            ctrl.controlTypePrefix.Length), out IsDetailNotMaster, false);

                    if (field != null)
                    {
                        LinkControlDataField(writer, ctrl, field, IsDetailNotMaster);
                        DataFieldUndoCapability(writer, ctrl, field, IsDetailNotMaster);
                    }
                }
            }
            else if (ctrl.controlTypePrefix == "uco")
            {
                AddChildUserControlExtraCalls(writer, ctrl);
            }
            else if (ctrl.HasAttribute("DynamicControlType"))
            {
                writer.Template.AddToCodelet("SAVEDATA", "if(FUco" + ctrl.controlName.Substring(
                        3) + " != null)" + Environment.NewLine + "{" + Environment.NewLine +
                    "    FUco" + ctrl.controlName.Substring(3) + ".GetDataFromControls();" + Environment.NewLine + "}" + Environment.NewLine);
                writer.Template.AddToCodelet("PRIMARYKEYCONTROLSREADONLY", "if(FUco" + ctrl.controlName.Substring(
                        3) + " != null)" + Environment.NewLine + "{" + Environment.NewLine +
                    "    FUco" + ctrl.controlName.Substring(
                        3) + ".SetPrimaryKeyReadOnly(AReadOnly);" + Environment.NewLine + "}" + Environment.NewLine);
            }

            // Allow for adding of child-usercontrol extra calls *even* if the Template has got 'MasterTable' or 'DetailTable' attribute(s) [see above for those checks]
            if ((ctrl.controlTypePrefix == "uco")
                && (writer.CodeStorage.GetAttribute("DependentChildUserControl") == "true"))
            {
                AddChildUserControlExtraCalls(writer, ctrl);
            }

            // the readonly property eg of Textbox still allows tooltips and copy to clipboard, which enable=false would not allow
            if (TYml2Xml.HasAttribute(ctrl.xmlNode, "ReadOnly")
                && (TYml2Xml.GetAttribute(ctrl.xmlNode, "ReadOnly").ToLower() == "true"))
            {
                if (FHasReadOnlyProperty)
                {
                    writer.SetControlProperty(ctrl,
                        "ReadOnly",
                        "true");
                    writer.SetControlProperty(ctrl,
                        "TabStop",
                        "false");
                }
                else
                {
                    writer.SetControlProperty(ctrl,
                        "Enabled",
                        "false");
                }
            }

            if (GenerateDataValidationCode(writer, ctrl, out AutomDataValidation, out ReasonForAutomValidation))
            {
                AssignEventHandlerToControl(writer, ctrl, "Validated", "ControlValidatedHandler");
            }
            else
            {
                bool AssignControlUpdateDataHandler = false;

                if (ctrl.HasAttribute("DataField"))
                {
                    TDataBinding.GetTableField(ctrl, ctrl.GetAttribute("DataField"), out IsDetailNotMaster, true);

                    if (IsDetailNotMaster)
                    {
                        AssignControlUpdateDataHandler = true;
                    }
                }
                else
                {
                    if (writer.CodeStorage.HasAttribute("DetailTable"))
                    {
                        if (!((this is LabelGenerator) || (this is LinkLabelGenerator)))
                        {
                            if (TDataBinding.GetTableField(ctrl, ctrl.controlName.Substring(
                                        ctrl.controlTypePrefix.Length), out IsDetailNotMaster, false) != null)
                            {
                                if (IsDetailNotMaster)
                                {
                                    AssignControlUpdateDataHandler = true;
                                }
                            }
                        }
                    }
                }

                if (AssignControlUpdateDataHandler)
                {
                    if ((!ctrl.controlTypePrefix.StartsWith("mn"))
                        && (!ctrl.controlTypePrefix.StartsWith("tb"))
                        && (ctrl.controlTypePrefix != "pnl")
                        && (ctrl.controlTypePrefix != "grp")
                        && (ctrl.controlTypePrefix != "grd")
                        && (ctrl.controlTypePrefix != "btn")
                        && (ctrl.controlTypePrefix != "stb")
                        && (ctrl.controlTypePrefix != "lbl"))
                    {
                        AssignEventHandlerToControl(writer, ctrl, "Validated", "ControlUpdateDataHandler");
                        writer.Template.SetCodelet("GENERATECONTROLUPDATEDATAHANDLER", "true");
                    }
                }
            }

            return writer.Template;
        }