private TreeNode CopyNewNode(TreeNode tnSrc)
        {
            var tnNew = new TreeNode();

            try
            {
                if (tnSrc.Nodes.Count > 0)
                {
                    var lsTNs = new List <TreeNode>();
                    foreach (TreeNode tn in tnSrc.Nodes)
                    {
                        lsTNs.Add(CopyNewNode(tn));
                    }
                    var oldTag   = tnSrc.Tag as ControlPropertyModel;
                    var cp       = oldTag.ControlType;
                    var objNewCP = new ControlPropertyModel {
                        ControlType = cp, Properties = PropertyGridManager.GetPropertyGridObject(cp)
                    };

                    tnNew = new TreeNode(tnSrc.Text, lsTNs.ToArray())
                    {
                        Tag = objNewCP, Name = tnSrc.Text
                    };
                }
                else
                {
                    var oldTag   = tnSrc.Tag as ControlPropertyModel;
                    var cp       = oldTag.ControlType;
                    var objNewCP = new ControlPropertyModel {
                        ControlType = cp, Properties = PropertyGridManager.GetPropertyGridObject(cp)
                    };
                    tnNew = new TreeNode(tnSrc.Text)
                    {
                        Tag = objNewCP, Name = tnSrc.Text
                    };
                }
            }
            catch (Exception ex)
            {
            }

            return(tnNew);
        }
        private void btnPropertyAdd_Click(object sender, EventArgs e)
        {
            var objControl = pgControlProps.SelectedObject;

            if (_blIsNewControl)
            {
                if (lbControlTypes.SelectedItem.ToString() == "Table")
                {
                    //insitilise row with cells
                    var objTab = objControl as TableGridClass;
                    if (objTab.NoOfColumn > 0)
                    {
                        var tRows = new List <TreeNode>();
                        if (objTab.IsDynamic)
                        {
                            var lsRowTds  = new List <TreeNode>();
                            var lsHeadTds = new List <TreeNode>();
                            for (int i = 0; i < objTab.NoOfColumn; i++)
                            {
                                lsHeadTds.Add(new TreeNode("Cell", new TreeNode[] { new TreeNode("Label")
                                                                                    {
                                                                                        Tag = new ControlPropertyModel {
                                                                                            ControlType = "Label", Properties = PropertyGridManager.GetPropertyGridObject("Label")
                                                                                        }
                                                                                    } })
                                {
                                    Tag = new ControlPropertyModel {
                                        ControlType = "Cell", Properties = new CellGridCalss {
                                            Width = objTab.ColumnWidths[i]
                                        }
                                    }
                                });
                                lsRowTds.Add(new TreeNode("Cell", new TreeNode[] { new TreeNode("Field")
                                                                                   {
                                                                                       Tag = new ControlPropertyModel {
                                                                                           ControlType = "Field", Properties = PropertyGridManager.GetPropertyGridObject("Field")
                                                                                       }
                                                                                   } })
                                {
                                    Tag = new ControlPropertyModel {
                                        ControlType = "Cell", Properties = new CellGridCalss {
                                            Width = objTab.ColumnWidths[i]
                                        }
                                    }
                                });
                            }
                            tRows.Add(PrepareTreeNode("Header", new ControlPropertyModel {
                                ControlType = "Row", Properties = new RowGridClass {
                                }
                            }, lsHeadTds));
                            tRows.Add(PrepareTreeNode("Items", new ControlPropertyModel {
                                ControlType = "Row", Properties = new RowGridClass {
                                }
                            }, lsRowTds));
                        }
                        else
                        {
                            var lsTds = new List <TreeNode>();
                            for (int i = 0; i < objTab.NoOfColumn; i++)
                            {
                                lsTds.Add(new TreeNode("Cell")
                                {
                                    Tag = new ControlPropertyModel {
                                        ControlType = "Cell", Properties = new CellGridCalss {
                                            Width = objTab.ColumnWidths[i]
                                        }
                                    }, ContextMenu = cmCell
                                });
                            }
                            tRows.Add(PrepareTreeNode("Row", new ControlPropertyModel {
                                ControlType = "Row", Properties = new RowGridClass {
                                }
                            }, lsTds));
                        }



                        var tab = PrepareTreeNode("Table", new ControlPropertyModel {
                            ControlType = "Table", Properties = objTab
                        }, tRows);


                        AddTreeNodeToDocument(tab);
                    }
                    else
                    {
                        MessageBox.Show("Parent table should have atleast one column");
                    }
                }
                else
                {
                    AddTreeNodeToDocument(PrepareTreeNode(lbControlTypes.SelectedItem.ToString(), new ControlPropertyModel {
                        ControlType = lbControlTypes.SelectedItem.ToString(), Properties = objControl
                    }));

                    var modelFieldInfo = PropertyGridManager.GetModelFieldName(new ControlPropertyModel {
                        ControlType = lbControlTypes.SelectedItem.ToString(), Properties = objControl
                    });
                    if (modelFieldInfo != "")
                    {
                        var flds = modelFieldInfo.Split('-');
                        if (lsModels.Where(x => x.ModelName == flds[0] && x.FieldName == flds[1]).FirstOrDefault() == null)
                        {
                            lsModels.Add(new GridModelProperty {
                                ModelName = flds[0], FieldName = flds[1]
                            });
                            RefreshModelGrid();
                        }
                    }
                }

                // AddTreeNodeToDocument(lbControlTypes.SelectedItem.ToString(), objControl);
            }
            else
            {
                //Update properties
                if (tvDocument.SelectedNode != null)
                {
                    var objControlUpdateObject = (tvDocument.SelectedNode.Tag as ControlPropertyModel);



                    objControlUpdateObject.Properties = pgControlProps.SelectedObject;

                    tvDocument.SelectedNode.Tag = objControlUpdateObject;

                    var modelFieldInfo = PropertyGridManager.GetModelFieldName(new ControlPropertyModel {
                        ControlType = objControlUpdateObject.ControlType, Properties = objControl
                    });
                    if (modelFieldInfo != "")
                    {
                        var flds = modelFieldInfo.Split('-');
                        if (lsModels.Where(x => x.ModelName == flds[0] && x.FieldName == flds[1]).FirstOrDefault() == null)
                        {
                            lsModels.Add(new GridModelProperty {
                                ModelName = flds[0], FieldName = flds[1]
                            });
                            RefreshModelGrid();
                        }
                    }

                    ShowPropertyGrid(false);
                }
            }

            lbControlTypes.ClearSelected();
        }
        private void btnAddControlProperty_Click(object sender, EventArgs e)
        {
            _blIsNewControl     = true;
            btnPropertyAdd.Text = "Add";
            var lsDirectAddControls = Master.DirectAddingControls();

            if (lbControlTypes.SelectedItem != null)
            {
                if (!lsDirectAddControls.Contains(lbControlTypes.SelectedItem.ToString()))
                {
                    //Open Property grid for inputs

                    pgControlProps.SelectedObject = PropertyGridManager.GetPropertyGridObject(lbControlTypes.SelectedItem.ToString());

                    ShowPropertyGrid(true);
                }
                else
                {
                    //directly add to treeview
                    if (tvDocument.SelectedNode != null)
                    {
                        if (lbControlTypes.SelectedItem.ToString() == "Empty")
                        {
                            AddTreeNodeToDocument(PrepareTreeNode("Empty", new ControlPropertyModel {
                                ControlType = "Row", Properties = new EmptyCellGridClass {
                                    Name = ""
                                }
                            }));
                        }
                        else if (lbControlTypes.SelectedItem.ToString() == "Row")
                        {
                            if (tvDocument.SelectedNode.Tag != null)
                            {
                                var objTab = ((tvDocument.SelectedNode.Tag as ControlPropertyModel).Properties as TableGridClass);
                                if (objTab.NoOfColumn > 0)
                                {
                                    var lsTds = new List <TreeNode>();
                                    for (int i = 0; i < objTab.NoOfColumn; i++)
                                    {
                                        lsTds.Add(new TreeNode("Cell")
                                        {
                                            Tag = new ControlPropertyModel {
                                                ControlType = "Cell", Properties = new CellGridCalss {
                                                    Width = objTab.ColumnWidths[i]
                                                }
                                            }, ContextMenu = cmCell
                                        });
                                    }

                                    AddTreeNodeToDocument(PrepareTreeNode("Row", new ControlPropertyModel {
                                        ControlType = "Row", Properties = new RowGridClass {
                                        }
                                    }, lsTds));
                                }
                                else
                                {
                                    MessageBox.Show("Parent table should have atleast one column");
                                }
                            }
                            else
                            {
                                MessageBox.Show("Parent should be table");
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Please select control");
            }
        }