Ejemplo n.º 1
0
        /// <summary>
        /// Generic new control from type
        /// </summary>
        /// <param name="control_guid"></param>
        /// <param name="type_control"></param>
        /// <returns></returns>
        public Control CreateControl(String control_guid, Control Model = null)
        {
            try
            {
                // Active control by guid
                view_desc.ActiveControl(control_guid);
                if (view_desc.Type_str == "")
                {
                    errors.add(String.Format("Error: control guid '{0}' not found in view: {1}", control_guid, Name));
                }

                if (view_desc.hasErrors())
                {
                    errors.add(view_desc.jErrors);

                    return(null);
                }
                else
                {
                    Control typed_control = null;
                    if (view_desc.Class_Path != "")
                    {
                        typed_control = CreateTypedControl_FromPath(control_guid, view_desc.Class_Path);
                    }
                    else
                    {
                        typed_control = (Model != null) ? Model : CreateTypedControl(control_guid, view_desc.Type_str);
                    }

                    if ((Model == null) && (typed_control.Controls.Count > 0))
                    {
                        Model = typed_control;
                    }
                    if (typed_control != null)
                    {
                        typed_control.Name = control_guid;

                        // Configure control
                        JToken cfg = Configure(view_desc, typed_control);

                        // add control to list
                        ctrls.Add(control_guid, new CtrlStruct(control_guid, typed_control, cfg, view_desc.Events, view_desc.Controls));

                        foreach (String child_control in view_desc.Controls)
                        {
                            Control cChild = ((Model != null) && (Model.Controls.Count > 0) && (Model.Controls[child_control] != null)) ? Model.Controls[child_control] : null;

                            Control newChild = CreateControl(child_control, cChild);
                            if (cChild == null)
                            {
                                typed_control.Controls.Add(newChild);
                            }
                        }
                        if ((Model != null) && (Model.ContextMenuStrip != null))
                        {
                            if (!ctrls.ContainsKey(Model.ContextMenuStrip.Name))
                            {
                                Control newChild = CreateControl(Model.ContextMenuStrip.Name, Model.ContextMenuStrip);
                            }
                        }
                    }
                    return(typed_control);
                }
            }
            catch (Exception exc)
            {
                errors.add(String.Format("Unhandled error creating control '{0}': {1}", control_guid, exc.Message));
                return(null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generic new control from type
        /// </summary>
        /// <param name="control_guid"></param>
        /// <param name="type_control"></param>
        /// <returns></returns>
        private Control NewControl(String control_guid, TBaseControls bCtrl)
        {
            try
            {
                debug.add("New control: " + control_guid);

                // Active control by guid
                bCtrl.ActiveControl(control_guid);
                if (bCtrl.Type_str == "")
                {
                    errors.add(String.Format("Error: control guid '{0}' not found in view: {1}", control_guid, Name));
                }

                if (bCtrl.hasErrors())
                {
                    errors.add(bCtrl.jErrors);

                    return(null);
                }
                else
                {
                    // create typed control
                    Control typed_control = null;
                    Type    type_control  = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes().Where(type => type.FullName == "System.Windows.Forms." + bCtrl.Type_str)).FirstOrDefault();
                    if (type_control == null)
                    {
                        type_control = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes().Where(type => type.FullName == bCtrl.Type_str)).FirstOrDefault();
                    }
                    if (type_control == null)
                    {
                        if (bCtrl.Type_str != null)
                        {
                            errors.add(String.Format("Error creating control '{0}' from type '{1}'.", control_guid, bCtrl.Type_str));
                        }
                        else
                        {
                            errors.add(String.Format("Error creating control '{0}' from empty type.", control_guid));
                        }
                        return(null);
                    }
                    else
                    {
                        typed_control      = (Control)Activator.CreateInstance(type_control);
                        typed_control.Name = control_guid;

                        // Configure control
                        JToken cfg = Configure(bCtrl, typed_control);

                        // add control to list
                        ctrls.Add(control_guid, new CtrlStruct(control_guid, typed_control, cfg, bCtrl.Events, bCtrl.Controls));

                        foreach (String child_control in bCtrl.Controls)
                        {
                            typed_control.Controls.Add(NewControl(child_control, bCtrl));
                        }
                    }
                    return(typed_control);
                }
            }
            catch (Exception exc)
            {
                errors.add(String.Format("Unhandled error creating control '{0}': {1}", control_guid, exc.Message));
                return(null);
            }
        }