/// <summary>
        /// Création et Initalisation des contrôles du formulaire
        /// </summary>
        private void GenerateForm()
        {
            #region Taille par défaut
            // Positions et Tailles par défaut
            int y_field         = 0;
            int x_field         = 0;
            int width_label     = 100;
            int height_label    = 10;
            int width_control   = 200;
            int height_control  = 25;
            int width_groueBox  = 100;
            int height_groueBox = 200; // il ne sera pas utilisé

            // Orientation par défaut
            Orientation orientation = Orientation.Vertical;
            #endregion

            #region Préparation de l'interface par Panel et GroupeBox
            // Initalisation de l'interface avec TabControl
            this.InitTabPageInterface();

            // Création des groupBox s'il existe
            Dictionary <string, Control> GroupesBoxMainContainers = new Dictionary <string, Control>();
            this.CreateGroupesBoxes(GroupesBoxMainContainers, width_groueBox, height_groueBox);
            #endregion

            // L'index de la touche Entrer
            int TabIndex = 0;

            // Affichage des champs par leurs type
            foreach (PropertyInfo item in ListeChampsFormulaire())
            {
                #region Recalcule des valeurs pardéfaut selon l'annotation de chauqe champs
                // l'annotation
                AffichageProprieteAttribute AffichagePropriete = (AffichageProprieteAttribute)item.GetCustomAttribute(typeof(AffichageProprieteAttribute));

                // Taile du Field
                int width_control_config = width_control;
                if (AffichagePropriete.WidthControl != 0)
                {
                    width_control_config = AffichagePropriete.WidthControl;
                }

                // Orientation
                Orientation orientation_config = orientation;
                if (AffichagePropriete.UseOrientationField)
                {
                    orientation_config = AffichagePropriete.OrientationField;
                }
                #endregion

                Control field_control = null;
                switch (item.PropertyType.Name)
                {
                    #region Champs String
                case "String":


                    StringFiled stringFiled = new StringFiled(
                        item,
                        orientation_config,
                        new Size(width_label, height_label),
                        new Size(width_control_config, height_control));
                    stringFiled.Location      = new System.Drawing.Point(x_field, y_field);
                    stringFiled.Name          = item.Name;
                    stringFiled.TabIndex      = ++TabIndex;
                    stringFiled.Text_Label    = AffichagePropriete.Titre;
                    stringFiled.FieldChanged += ControlPropriete_ValueChanged;
                    if (AffichagePropriete.isOblegatoir)
                    {
                        stringFiled.ValidatingFiled += textBoxString_Validating;
                    }
                    // Insertion à l'interface
                    this.ConteneurFormulaire.Controls.Add(stringFiled);
                    field_control = stringFiled;
                    break;
                    #endregion

                    #region Champs Int32
                case "Int32":

                    Int32Filed int32Filed = new Int32Filed(
                        item,
                        orientation_config,
                        new Size(width_label, height_label),
                        new Size(width_control_config, height_control));
                    int32Filed.Location      = new System.Drawing.Point(x_field, y_field);
                    int32Filed.Name          = item.Name;
                    int32Filed.TabIndex      = ++TabIndex;
                    int32Filed.Text_Label    = AffichagePropriete.Titre;
                    int32Filed.FieldChanged += ControlPropriete_ValueChanged;
                    if (AffichagePropriete.isOblegatoir)
                    {
                        int32Filed.ValidatingFiled += TextBoxInt32_Validating;
                    }
                    // Insertion à l'interface
                    this.ConteneurFormulaire.Controls.Add(int32Filed);
                    field_control = int32Filed;
                    break;
                    #endregion

                    #region Champs DateTime
                case "DateTime":

                    DateTimeField dateTimeField = new DateTimeField(
                        item,
                        orientation_config,
                        new Size(width_label, height_label),
                        new Size(width_control_config, height_control));
                    dateTimeField.Location      = new System.Drawing.Point(x_field, y_field);
                    dateTimeField.Name          = item.Name;
                    dateTimeField.TabIndex      = ++TabIndex;
                    dateTimeField.Text_Label    = AffichagePropriete.Titre;
                    dateTimeField.FieldChanged += ControlPropriete_ValueChanged;
                    if (AffichagePropriete.isOblegatoir)
                    {
                        dateTimeField.ValidatingFiled += DateTimePicker_Validating;
                    }
                    // Insertion à l'interface
                    this.ConteneurFormulaire.Controls.Add(dateTimeField);
                    field_control = dateTimeField;
                    break;
                    #endregion

                default:
                {
                    #region Champs : ManyToOne
                    if (AffichagePropriete.Relation == "ManyToOne")
                    {
                        // Déterminer le contenue du Field ManyToOne : GroupeBox ou Panel
                        Control ConteneurManyToMany = this.ConteneurFormulaire;
                        if (AffichagePropriete.GroupeBox != null && AffichagePropriete.GroupeBox != string.Empty)
                        {
                            ConteneurManyToMany = GroupesBoxMainContainers[AffichagePropriete.GroupeBox];
                        }

                        // Création de champs ManyToOneField
                        Int64 InitValue = 0;
                        //if (ValeursFiltre != null && ValeursFiltre.Keys.Contains(propertyInfo.Name))
                        //    default_value = (Int64)ValeursFiltre[propertyInfo.Name];

                        ManyToOneField manyToOneField = new ManyToOneField(this.Service, item,
                                                                           ConteneurManyToMany, orientation_config,
                                                                           new Size(width_label, height_label),
                                                                           new Size(width_control_config, height_control), InitValue
                                                                           );
                        manyToOneField.Location      = new System.Drawing.Point(x_field, y_field);
                        manyToOneField.Name          = item.Name;
                        manyToOneField.TabIndex      = ++TabIndex;
                        manyToOneField.Text_Label    = AffichagePropriete.Titre;
                        manyToOneField.FieldChanged += ControlPropriete_ValueChanged;
                        if (AffichagePropriete.isOblegatoir)
                        {
                            manyToOneField.Validating += ComboBox_Validating;
                        }
                        this.ConteneurFormulaire.Controls.Add(manyToOneField);
                        field_control = manyToOneField;
                    }
                    #endregion

                    #region Champs ManyToMany
                    if (AffichagePropriete.Relation == "ManyToMany")
                    {
                        //Trouver les Valeurs par défaut
                        List <BaseEntity> ls_default_value = null;
                        if (this.Entity != null)
                        {
                            IList ls_obj = item.GetValue(this.Entity) as IList;

                            if (ls_obj != null)
                            {
                                ls_default_value = ls_obj.Cast <BaseEntity>().ToList();
                            }
                        }

                        InputCollectionControle InputCollectionControle = new InputCollectionControle(this.Service, item, ls_default_value, this.Entity);
                        InputCollectionControle.Name          = item.Name;
                        InputCollectionControle.ValueChanged += ControlPropriete_ValueChanged;

                        if (AffichagePropriete.TabPage)
                        {
                            InputCollectionControle.Dock = DockStyle.Fill;
                            TabPage tabPage = new TabPage();
                            tabPage.Name = "tabPage" + item.Name;
                            tabPage.Text = AffichagePropriete.Titre;
                            tabPage.Controls.Add(InputCollectionControle);
                            tabControlForm.TabPages.Add(tabPage);
                        }
                        else
                        {
                            this.ConteneurFormulaire.Controls.Add(InputCollectionControle);
                            field_control = InputCollectionControle;
                        }
                    }
                    #endregion
                }
                break;
                } // Fin de suitch

                // Insertion du Champs dans sa GroupeBox si il existe
                // [Bug] il n'est pas supprimé de l'inrface, car il est déja ajouté
                if (field_control != null && AffichagePropriete.GroupeBox != null && AffichagePropriete.GroupeBox != string.Empty)
                {
                    GroupesBoxMainContainers[AffichagePropriete.GroupeBox].Controls.Add(field_control);
                }
            }// Fin de for

            // TabControl sur Enregistrer et Annuler
            this.btEnregistrer.TabIndex = ++TabIndex;
            this.btAnnuler.TabIndex     = ++TabIndex;


            foreach (GroupBox item in this.ConteneurFormulaire.Controls.Cast <Control>().Where(c => c.GetType() == typeof(GroupBox)))
            {
                // item.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold);
                item.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
            }
            foreach (FlowLayoutPanel item in GroupesBoxMainContainers.Values)
            {
                item.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
            }
        }
示例#2
0
        /// <summary>
        /// Lire les informations du formulaire vers l'Entity
        /// </summary>
        protected virtual void ReadFormToEntity()
        {
            BaseEntity entity     = this.Entity;
            Type       typeEntity = this.Service.TypeEntity;

            foreach (PropertyInfo item in ListeChampsFormulaire())
            {
                AffichageProprieteAttribute AffichagePropriete = (AffichageProprieteAttribute)item.GetCustomAttribute(typeof(AffichageProprieteAttribute));
                Type   typePropriete = item.PropertyType;
                string NomPropriete  = item.Name;

                #region Read :String Field
                if (typePropriete.Name == "String")
                {
                    string value = "";
                    if (this.AutoGenerateField)
                    {
                        BaseField baseField = this.FindGenerateField(item.Name);
                        value = baseField.Value.ToString();
                    }
                    else
                    {
                        TextBox txtBox = (TextBox)this.FindPersonelField(item.Name, "TextBox");
                        value = txtBox.Text;
                    }
                    typeEntity.GetProperty(NomPropriete).SetValue(entity, value);
                }
                #endregion

                #region Read : Int32 Field
                if (item.PropertyType.Name == "Int32")
                {
                    int Nombre = 0;
                    if (this.AutoGenerateField)
                    {
                        BaseField baseField = this.FindGenerateField(item.Name);
                        Nombre = Convert.ToInt32(baseField.Value);
                    }
                    else
                    {
                        TextBox txtBox = (TextBox)this.FindPersonelField(item.Name, "TextBox");
                        if (!int.TryParse(txtBox.Text, out Nombre))
                        {
                            MessageBox.Show("Impossible de lire un nombre :" + txtBox.Text);
                        }
                    }
                    typeEntity.GetProperty(NomPropriete).SetValue(entity, Nombre);
                }
                #endregion

                #region Read : DateTime Field
                if (typePropriete.Name == "DateTime")
                {
                    DateTime date = DateTime.MinValue;
                    if (this.AutoGenerateField)
                    {
                        BaseField baseField = this.FindGenerateField(item.Name);
                        date = Convert.ToDateTime(baseField.Value);
                    }
                    else
                    {
                        DateTimePicker dateTimePicker = (DateTimePicker)this.FindPersonelField(item.Name, "TextBox");
                        date = dateTimePicker.Value;
                    }
                    typeEntity.GetProperty(NomPropriete).SetValue(entity, date);
                }
                #endregion

                #region Read : ManyToOne Field
                if (AffichagePropriete.Relation == "ManyToOne")
                {
                    Int64 id;
                    if (this.AutoGenerateField)
                    {
                        BaseField baseField = this.FindGenerateField(item.Name);
                        id = Convert.ToInt64(baseField.Value);
                    }
                    else
                    {
                        ComboBox comboBox = (ComboBox)this.FindPersonelField(item.Name, "ComboBox");
                        id = Convert.ToInt64(comboBox.SelectedValue);
                    }
                    IBaseRepository ServicesEntity  = this.Service.CreateInstance_Of_Service_From_TypeEntity(item.PropertyType);
                    BaseEntity      ManyToOneEntity = ServicesEntity.GetBaseEntityByID(Convert.ToInt32(id));
                    typeEntity.GetProperty(NomPropriete).SetValue(entity, ManyToOneEntity);
                }
                #endregion

                #region  Read : ManyToMany
                if (AffichagePropriete.Relation == "ManyToMany")
                {
                    List <BaseEntity> ls = null;
                    if (this.AutoGenerateField)
                    {
                        InputCollectionControle inputCollectionControle = null;
                        if (AffichagePropriete.TabPage)
                        {
                            Control[] recherche = this.tabControlForm.Controls.Find(item.Name, true);
                            if (recherche.Count() > 0)
                            {
                                inputCollectionControle = (InputCollectionControle)recherche.First();
                            }
                            else
                            {
                                throw new FieldNotExistInFormException();
                            }
                        }
                        else
                        {
                            Control[] recherche = this.ConteneurFormulaire.Controls.Find(item.Name, true);
                            if (recherche.Count() > 0)
                            {
                                inputCollectionControle = (InputCollectionControle)recherche.First();
                            }
                            else
                            {
                                throw new FieldNotExistInFormException();
                            }
                        }

                        ls = inputCollectionControle.Value;
                    }
                    else
                    {
                        ListBox comboBox = (ListBox)this.FindPersonelField(item.Name, "ListBox");
                        ls = comboBox.Items.Cast <BaseEntity>().ToList <BaseEntity>();
                    }


                    IBaseRepository ServicesEntity = this.Service.CreateInstance_Of_Service_From_TypeEntity(item.PropertyType.GetGenericArguments()[0], this.Service.Context());


                    Type  TypeListeObjetValeur = typeof(List <>).MakeGenericType(item.PropertyType.GetGenericArguments()[0]);
                    IList ls_valeur            = (IList)Activator.CreateInstance(TypeListeObjetValeur);



                    foreach (BaseEntity b in ls)
                    {
                        var entity_valeur = ServicesEntity.GetBaseEntityByID(b.Id);
                        ls_valeur.Add(entity_valeur);
                    }


                    typeEntity.GetProperty(NomPropriete).SetValue(entity, ls_valeur);
                }
                #endregion
            }
        }