/// <summary>Generates the controls on the designer panels.</summary> /// <param name="form">The form</param> /// <param name="classObject">The class objectect</param> /// <param name="panel1">The panel1</param> /// <param name="panel2">The panel to setting. Default value = null.</param> /// <param name="panel3">The panel3 setting. Default value = null.</param> /// <param name="onValueChange">The on valueue change setting. Default value = null.</param> /// <returns>bool</returns> public bool Generate_Controls(Form form, IObjectModel classObject, Panel panel1, Panel panel2 = null, Panel panel3 = null, EventHandler <onInputControl_EventArgs> onValueChange = null) { var type = classObject.GetType(); BlueprintData_TableAttribute attribute; LamedalCore_.Instance.Types.Class.ClassAttributes.Find_Class(type, out attribute, new BlueprintData_TableAttribute()); stateForm state = form.zzState(); state.onValueChange = onValueChange; state.FormObjectModel = classObject; state.DoEventsFlag = false; IDesignerHost host = UIDesigner_Service.IDesignerHost_FromForm(form); int height = Lamedal_WinForms.Instance.libUI.WinForms.FormGenerate.Generate_Controls(host, type, attribute, panel1, panel2, panel3, OnValueChangeEvent); form.Height = height; if (attribute != null) { if (attribute.Caption != "") { form.Text = attribute.Caption; } } state.DoEventsFlag = true; return(true); }
/// <summary> /// Setups the model and Form fields. /// </summary> /// <param name="form">The form</param> /// <param name="model">The IO bject model</param> /// <param name="fieldName">The field name</param> /// <param name="control">The object model iucontrol</param> /// <param name="id">The identifier setting. Default value = -1.</param> /// <code GenerateParameter1="model"></code> public void ModelField(Form form, IObjectModel model, string fieldName, IUControl_ObjectModel control, int id = -1) { // Double check value before assignment! // ===================================== var type = model.GetType(); BlueprintData_FieldAttribute[] fieldDefinition = null; FieldInfo fieldInfo1 = type.GetField(fieldName); if (fieldInfo1 != null) { fieldDefinition = (BlueprintData_FieldAttribute[])fieldInfo1.GetCustomAttributes(typeof(BlueprintData_FieldAttribute), false); } else { PropertyInfo propertyInfo = type.GetProperty(fieldName); if (propertyInfo != null) { fieldDefinition = (BlueprintData_FieldAttribute[])propertyInfo.GetCustomAttributes(typeof(BlueprintData_FieldAttribute), false); } } if (fieldDefinition == null) { var errMsg = "Error! Field '{0}.{1}' does not exist!".zFormat(type.ToString(), fieldName); throw new ArgumentException(errMsg, nameof(fieldName)); } // Check the ID value // ================== var fieldId = fieldDefinition.Length > 0 ? fieldDefinition[0].Id : -1; if (id != -1) { if (id != fieldId) { string msg = "Error in object mapping!".NL() + "Field '{0}.{1}'".zFormat(type.ToString(), fieldName).NL() + "{0} id = {1} (and not {2} as expected)".zFormat(fieldName, fieldId, id); throw new ArgumentException(msg, nameof(id)); } } control.Field_Name = fieldName; // Map the field; // Update the field value on the form Lamedal_WinForms.Instance.libUI.WinForms.FormGenerate.Form_FromObject(form, model, fieldName); // Sync form }
/// <summary> /// Converts the generated form to object. /// </summary> /// <param name="e">The input event arguments control</param> /// <param name="Object">The object</param> public void Form_ToObject(onInputControl_EventArgs e, IObjectModel Object) { // Value on the form has changed -> update the object Input_Control input = e.Control_; string fieldName = input.Field_Name; string fieldValue = input.Field_Value; FieldInfo fieldInfo = Object.GetType().GetField(fieldName); Type fieldType = fieldInfo.FieldType; object value; if (fieldValue.zIsNullOrEmpty()) { value = LamedalCore_.Instance.Types.Object.DefaultValue(fieldType); } else { value = LamedalCore_.Instance.Types.Object.CastTo(fieldValue, fieldType); } fieldInfo.SetValue(Object, value); }
/// <summary>Generates the controls on the designer panels.</summary> /// <param name="classObject">The class objectect</param> /// <param name="onValueChange">The on valueue change setting. Default value = null.</param> /// <param name="panels">The panels setting. Default value = OnePanel.</param> /// <returns>Form</returns> public Form Form_Generate(IObjectModel classObject, EventHandler <onInputControl_EventArgs> onValueChange = null, enForm_Panels panels = enForm_Panels.OnePanel) { BlueprintData_TableAttribute attribute; LamedalCore_.Instance.Types.Class.ClassAttributes.Find_Class(classObject.GetType(), out attribute, new BlueprintData_TableAttribute()); if (attribute != null) { panels = (enForm_Panels)attribute.TotalPanels; } if (panels == enForm_Panels.OnePanel) // Create form with 1 panel { var frmDialog1 = new Form_ClassDialog1(); if (Generate_Controls(frmDialog1, classObject, frmDialog1.panel2, onValueChange: onValueChange)) { return(frmDialog1); } } if (panels == enForm_Panels.TwoPanels) // Create form with 2 panels { var frmDialog2 = new Form_ClassDialog2(); if (Generate_Controls(frmDialog2, classObject, frmDialog2.panel2, frmDialog2.panel3, onValueChange: onValueChange)) { return(frmDialog2); } } if (panels == enForm_Panels.TreePanels) // Create form with 3 panels { var frmDialog3 = new Form_ClassDialog3(); if (Generate_Controls(frmDialog3, classObject, frmDialog3.panel2, frmDialog3.panel3, frmDialog3.panel4, onValueChange)) { return(frmDialog3); } } return(null); }
/// <summary> /// Resets every value in the model to default values. /// </summary> /// <param name="Object">The object</param> public void ModelReset(IObjectModel Object) { var type = Object.GetType(); // Set the fields FieldInfo[] fields = LamedalCore_.Instance.Types.Class.ClassInfo.Fields_AsFieldInfo(type); foreach (FieldInfo field in fields) { object value = LamedalCore_.Instance.Types.Object.DefaultValue(type); field.SetValue(Object, value); } // Set the properties PropertyInfo[] properties = LamedalCore_.Instance.Types.Class.ClassInfo.Properties_AsPropertyInfo(type); foreach (PropertyInfo property in properties) { object value = LamedalCore_.Instance.Types.Object.DefaultValue(type); var setter = property.GetSetMethod(); if (setter != null) { property.SetValue(Object, value, null); } } }
/// <summary> /// Update the generated form from the object. /// </summary> /// <param name="form">The form</param> /// <param name="Object">The object</param> /// <param name="fieldName">Name of the field.</param> public void Form_FromObject(Form form, IObjectModel Object, string fieldName = "") { if (Object == null) { var ex = new ArgumentException("Error! The object can not be null!", nameof(Object)); ex.zLogLibraryMsg(); throw ex; } var type = Object.GetType(); var controls = IamWindows.libUI.WinForms.Controls.Control.FindComponents <IUControl_ObjectModel>(form); List <IUControl_ObjectModel> controlFields; if (fieldName == "") { controlFields = controls.Where(x => x.Field_Name != null).ToList(); } else { controlFields = controls.Where(x => x.Field_Name == fieldName).ToList(); } FieldInfo[] fields = LamedalCore_.Instance.Types.Class.ClassInfo.Fields_AsFieldInfo(type); PropertyInfo[] properties = LamedalCore_.Instance.Types.Class.ClassInfo.Properties_AsPropertyInfo(type); foreach (IUControl_ObjectModel control in controlFields) { // Find the field or the property form the object and // get the value // ================================================= object value = null; fieldName = control.Field_Name; FieldInfo field = fields.FirstOrDefault(x => x.Name == fieldName); if (field != null) { value = field.GetValue(Object); } else { PropertyInfo property = properties.FirstOrDefault(x => x.Name == fieldName); if (property == null) { var errMsg = "Error! Unable to find form control in the model for field name '{0}'.".zFormat(fieldName); throw new Exception(errMsg); } value = property.GetValue(Object, null); } // Assign the value to the control on the form // =========================================== //var test = value == null; //if (test == false) test = value is string; if (control is ListBox /*&& test == false*/) { var listbox = control as ListBox; if (value == null) { listbox.Items.Clear(); } else { var listValues = (IList)value; if (listbox.Items.Count != listValues.Count) { listbox.Items.zFrom_IList(listValues); } } } else { string fieldValue = value.zObject().AsStr(); control.Field_Value = fieldValue; } } }