private void Image_Changed(object sender, EventArgs e) { ScrollPicture sp = (ScrollPicture)sender; int i = Controls.IndexOf(sp); if (sp.Image != null) { Size s = sp.Image.Size; Size t = new Size { Height = Math.Min(s.Height, Controls[i + 1].Top - sp.Top), }; t.Width = s.Width * t.Height / s.Height; sp.Size = t; } }
//public MiseAJourObjet(object objet, Modèle md) : this(objet, md, null) //{ //} public MiseAJourObjet(object objet, TreeNode tn = null) { if (objet == null) { return; } if (tn != null) { currentNode = tn; } Cursor = Cursors.WaitCursor; InitializeComponent(); ControlBox = true; FormBorderStyle = FormBorderStyle.FixedSingle; // musiciens = md.Musicien.OrderBy(c => c.Nom_Musicien).ThenBy(c => c.Prénom_Musicien).ToList(); Type tp = objet.GetType(); if (tp.FullName.Contains("Proxies")) { tp = tp.BaseType; } Text = "Modification " + tp.Name; ObjetCourant = objet; int y = 30; int largeur = 0; var mo = tp.GetMethod("ToString") /*.ToList().Where(c => c.Name == "ToString")*/; string a = (string)mo.Invoke(objet, null); Text += " " + (string)mo.Invoke(objet, null); #region Propriétés PropertyInfo[] pis = tp.GetProperties().Where(p => !p.Name.StartsWith("Code_")).ToArray(); foreach (PropertyInfo propriété in pis) { Label lb = new Label { Text = propriété.Name.Replace("_", " "), Location = new Point(10, y) }; Control edit = null; Control edit2 = null; Control but = null; if (propriété.PropertyType.Name.Contains("IColl")) { #region Traitement des collections if (objet is Opéra opera) { #region Cas d'un opéra if (propriété.Name == "Roles") { Controls.Add(lb); DataGridView dtg = new DataGridView { Name = "Roles", AllowUserToAddRows = true, AllowUserToDeleteRows = true }; dtg.UserAddedRow += Dtg_UserAddedRow; dtg.RowValidating += Dtg_RowValidating; dtg.RowValidated += Dtg_RowValidated; dtg.UserDeletedRow += Dtg_UserDeletedRow; dtg.Tag = opera; dtg.AutoResizeColumns(); var piss = typeof(Roles).GetProperties(); foreach (PropertyInfo pi in piss) { if (pi.PropertyType.Name == "String") { dtg.Columns.Add(pi.Name, pi.Name); } } if (opera.Roles.Count != 0) { dtg.Rows.Add(opera.Roles.Count); int i = 0; foreach (Roles r in opera.Roles) { dtg.Rows[i].Tag = r; foreach (PropertyInfo pi in piss) { if (pi.PropertyType.Name == "String") { dtg.Rows[i].Cells[pi.Name].Value = pi .GetValue(r); } } i++; } } else { dtg.Rows.Add(1); } dtg.Height = 20 * (dtg.RowCount + 2); edit = dtg; } #endregion } if (objet is Disques disque) { if (propriété.Name == "LienYouTube") { if ((disque.Editeur != null) && ((disque.Editeur.NomEditeur == "You tube") | (disque.LienYouTube.Count != 0))) { Controls.Add(lb); DataGridView dtg = new DataGridView { Name = "LienYouTube", AllowUserToAddRows = true, AllowUserToDeleteRows = true, Tag = disque }; dtg.UserAddedRow += Dtg_UserAddedRow; dtg.RowValidating += Dtg_RowValidating; dtg.RowValidated += Dtg_RowValidated; dtg.UserDeletedRow += Dtg_UserDeletedRow; dtg.AutoResizeColumns(); var piss = typeof(LienYouTube).GetProperties(); foreach (PropertyInfo pi in piss) { if (pi.PropertyType.Name == "String") { dtg.Columns.Add(pi.Name, pi.Name); } } if (disque.LienYouTube.Count != 0) { dtg.Rows.Add(disque.LienYouTube.Count); int i = 0; foreach (LienYouTube r in disque.LienYouTube) { dtg.Rows[i].Tag = r; foreach (PropertyInfo pi in piss) { if (pi.PropertyType.Name == "String") { dtg.Rows[i].Cells[pi.Name].Value = pi .GetValue(r); } } i++; } } else { dtg.Rows.Add(1); // dtg.Rows[0].Cells.Add(new Cell); } // dtg.Columns[0].Width = ((string) dtg.Rows[0].Cells[0].Value).Length * 6/* ((LienYouTube)disque.LienYouTube.First()).YouTube.Length*5; */; dtg.Height = 20 * (dtg.RowCount + 2); dtg.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; edit = dtg; } } } #endregion } else if ((propriété.SetMethod != null) & !propriété.Name.Contains("Code")) { #region Traitement des propriétes simples switch (propriété.Name) { #region En fonction du nom de propriété case "Musicien": #region Musicien edit = new ComboBox { Name = propriété.Name, Width = 180 }; //if (ObjetCourant is Opéra) // musiciens = MainForm.md.Musicien.Where(c => c.Instrument.Nom_Instrument == "Composition").OrderBy(c => c.Nom_Musicien).ToList(); // if (ObjetCourant.GetType().Name.Contains("Disques") | ObjetCourant.GetType().Name.Contains("Interprétation") | ObjetCourant.GetType().Name.Contains("Diriger") | ObjetCourant.GetType().Name.Contains("MiseEnScene")) //{ // musiciens = MainForm.md.Musicien.Where(c => c.Instrument.Type == "Voix" | c.Instrument.Nom_Instrument == "Direction" | c.Instrument.Nom_Instrument == "Metteur En Scène").OrderBy(c => c.Nom_Musicien).ToList(); //} switch (ObjetCourant) { case Opéra opera: musiciens = MainForm.md.Musicien.Where(c => c.Instrument.Nom_Instrument == "Composition").OrderBy(c => c.Nom_Musicien).ToList(); break; case Disques disque: case Interprétation inter: case Diriger dir: case MiseEnScene mise: musiciens = MainForm.md.Musicien.Where(c => c.Instrument.Type == "Voix" | c.Instrument.Nom_Instrument == "Direction" | c.Instrument.Nom_Instrument == "Metteur En Scène").OrderBy(c => c.Nom_Musicien).ToList(); break; } musiciens.ForEach(x => ((ComboBox)edit).Items.Add(x)); ((ComboBox)edit).SelectedItem = (Musicien)propriété.GetValue(objet); #endregion but = CréeBouton(propriété); break; case "Opéra": edit = CréeCombo(objet, propriété, tn); but = CréeBouton(propriété); break; case "Editeur": case "Instrument": case "Orchestres": case "Pays": case "Airs": edit = CréeCombo(objet, propriété); but = CréeBouton(propriété); break; case "Salles": edit = CréeCombo(objet, propriété); but = CréeBouton(propriété); break; case "Ville": case "Vidéo": case "Audio": case "Définition": edit = CréeCombo(objet, propriété); break; case "Détail_Définition": case "Format": case "Source": edit = CréeCombo(objet, propriété); break; case "Roles": #region Role edit = new ComboBox { Width = 180, Name = propriété.Name }; int op = ((Interprétation)objet).Disques.Opéra.Code_Opéra; foreach (Roles pay in MainForm.md.Roles.Where(x => x.Opéra.Code_Opéra == op).OrderBy(c => c.Role)) { ((ComboBox)edit).Items.Add(pay); } Roles curRole = (Roles)propriété.GetValue(objet); ((ComboBox)edit).SelectedItem = curRole; but = new Button { Text = propriété.Name, Tag = propriété }; but.Click += But_Click; #endregion break; case "Disques": if (objet is Marqueurs marq) { break; } // else { edit = CréeCombo(((Disques)marq.Disques).Opéra, md, propriété); } edit = CréeCombo(objet, propriété); break; #endregion default: #region En fonction directe des types de données switch (propriété.PropertyType.Name) { case "Boolean": edit = new CheckBox(); break; case "Single": case "Double": #region Nombre edit = new NumericUpDown { Width = 50, Maximum = 1000, DecimalPlaces = 2 }; if (propriété.GetValue(objet) != null) { ((NumericUpDown)edit).Text = propriété.GetValue(objet).ToString(); } #endregion break; case "Point": #region Point edit = new NumericUpDown(); ((NumericUpDown)edit).Minimum = -3000; ((NumericUpDown)edit).Maximum = 3000; if (propriété.GetValue(objet) != null) { ((NumericUpDown)edit).Value = ((Point)propriété.GetValue(objet)).X; } edit2 = new NumericUpDown(); ((NumericUpDown)edit2).Minimum = -3000; ((NumericUpDown)edit2).Maximum = 3000; if (propriété.GetValue(objet) != null) { ((NumericUpDown)edit2).Value = ((Point)propriété.GetValue(objet)).Y; } #endregion break; case "Size": #region Taille edit = new NumericUpDown(); ((NumericUpDown)edit).Minimum = 0; ((NumericUpDown)edit).Maximum = Screen.PrimaryScreen.Bounds.Width; if (propriété.GetValue(objet) != null) { ((NumericUpDown)edit).Value = ((Size)propriété.GetValue(objet)).Width; } edit.Width = 50; edit2 = new NumericUpDown(); ((NumericUpDown)edit2).Minimum = 0; ((NumericUpDown)edit2).Maximum = Screen.PrimaryScreen.Bounds.Height; if (propriété.GetValue(objet) != null) { ((NumericUpDown)edit2).Value = ((Size)propriété.GetValue(objet)).Height; } edit2.Width = 50; break; #endregion case "String": #region Chaines edit = new TextBox { Name = propriété.Name }; TextBox Editeur = (TextBox)edit; if (propriété.GetValue(objet) != null) { edit.Text = (String)propriété.GetValue(objet); if (edit.Text.Length > 100) { edit.Size = new Size(400, 100); ((TextBox)edit).Multiline = true; ((TextBox)edit).ScrollBars = ScrollBars.Both; } if ((edit.Text.Length > 20) & (edit.Text.Length < 100)) { edit.Size = new Size(300, 20); ((TextBox)edit).ScrollBars = ScrollBars.Horizontal; } } StringLengthAttribute strLenAttr = propriété.GetCustomAttributes(typeof(StringLengthAttribute), false).Cast <StringLengthAttribute>().SingleOrDefault(); if (strLenAttr == null) // Seul Argument n'a pas de longueur maximale définie { Editeur.Multiline = true; Editeur.ScrollBars = ScrollBars.Vertical; edit.TextChanged += Edit_TextChanged; edit.Size = new Size(300, 20); } break; #endregion case "Int16": case "Int32": #region Entier edit = new NumericUpDown(); ((NumericUpDown)edit).Maximum = 10000; ((NumericUpDown)edit).Minimum = -13000; if (propriété.GetValue(objet) != null) { ((NumericUpDown)edit).Value = (int)propriété.GetValue(objet); } #endregion break; case "Color": #region Couleur edit = new Label(); ((Label)edit).BorderStyle = BorderStyle.Fixed3D; edit.Click += Couleur_Click; if (propriété.GetValue(objet) != null) { edit.BackColor = (Color)propriété.GetValue(objet); } break; #endregion case "Byte[]": #region Image edit = new ScrollPicture(); ((ScrollPicture)edit).Changed += Image_Changed; ((ScrollPicture)edit).BorderStyle = BorderStyle.Fixed3D; ((ScrollPicture)edit).SetImage((Byte[])propriété.GetValue(objet)); edit.Size = new Size(60, 80); #endregion ; break; default: #region Générique Type propertyType = propriété.PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { } Type t = Nullable.GetUnderlyingType(propriété.PropertyType); if (t != null) { if (propriété.Name.StartsWith("An")) { switch (t.Name) { case "Int32": edit = new NumericUpDown { Width = 50, Maximum = 3000, DecimalPlaces = 0 }; if (propriété.GetValue(objet) != null) { ((NumericUpDown)edit).Text = propriété.GetValue(objet).ToString(); } break; } } if (propriété.Name == "Durée") { switch (t.Name) { case "Int32": edit = new NumericUpDown { Width = 50, Maximum = 3000, DecimalPlaces = 0 }; if (propriété.GetValue(objet) != null) { ((NumericUpDown)edit).Text = propriété.GetValue(objet).ToString(); } break; } } } else { } break; #endregion ; } #endregion break; } if (edit != null) { edit.Tag = propriété; } #endregion } if (edit != null) { #region Création des contrôles edit.Location = new Point(lb.Location.X + lb.Width + 10, y); if (propriété.PropertyType.Name == "Byte[]") { edit.Location = new Point(lb.Location.X + lb.Width + 10, y); ScrollPicture sp = (ScrollPicture)edit; if (sp.Image != null) { Size s = sp.Image.Size; int scale = s.Height / 120; if (scale == 0) { scale = 1; } sp.Size = new Size(s.Width / scale, s.Height / scale); } ((Control)edit).Location = new Point(lb.Location.X + lb.Width + 10, y); } if (edit is DataGridView dtg) { edit.Width = 400; } Controls.Add(lb); Controls.Add(edit); largeur = edit.Left + edit.Width + 10; if (edit2 != null) { edit2.Tag = edit; edit2.Width = 50; edit2.Location = new Point(edit.Location.X + edit.Width + 10, y); Controls.Add(edit2); largeur = Math.Max(largeur, edit2.Left + edit2.Width + 10); } if (but != null) { but.Location = new Point(edit.Location.X + edit.Width + 10, y); Controls.Add(but); largeur = Math.Max(largeur, but.Left + but.Width + 10); } y += edit.Height + 10; #endregion } } #endregion Height = y + 50; #region Boutons y = 30; largeur += 120; foreach (string s in new string[] { "OK", "Annuler" }) { Button bouton = new Button { Text = s }; Controls.Add(bouton); bouton.Location = new Point(largeur, y); bouton.Click += Bouton_Click; y += bouton.Height + 5; Width = bouton.Left + bouton.Width + 40; } Height = Math.Max(Height, Bottom); #endregion Cursor = Cursors.Default; }