Example #1
0
 public Form1()
 {
     Controls.Add(p);
     InitializeComponent();
 }
Example #2
0
        public void Update(ApplicationDataContext applicationDataContext, bool NeedToCreate)
        {
            Form form = Controls.AddForm(this, ControlWidth, ControlHeight);
            int  y    = 0;

            GetProperties(form, this, ref y);
            GetFields(form, this, ref y, applicationDataContext);
            Button saveButton = Controls.AddButton(form, y, 0, ControlWidth, ControlHeight, "Сохранить");

            saveButton.Click += (sender, e) =>
            {
                int i = 1;
                foreach (PropertyInfo property in this.GetType().GetProperties())
                {
                    if (property.PropertyType.IsEnum)
                    {
                        property.SetValue(this, Enum.Parse(property.PropertyType, form.Controls[i].Text));
                    }
                    else
                    {
                        property.SetValue(this, Convert.ChangeType(form.Controls[i].Text, property.PropertyType));
                    }
                    i += 2;
                }
                foreach (FieldInfo field in this.GetType().GetFields())
                {
                    if (!field.FieldType.IsGenericType && field.FieldType.GetCustomAttributes(true).OfType <CommunicationTypeAttribute>().First().CommunicationType == "Агрегация")
                    {
                        ComboBox comboBox = (ComboBox)form.Controls[i];
                        if (comboBox.SelectedItem != null)
                        {
                            if (field.GetValue(this) != null)
                            {
                                foreach (var el in field.FieldType.GetFields())
                                {
                                    if (el.FieldType.IsGenericType)
                                    {
                                        IList list = (IList)el.GetValue(field.GetValue(this));
                                        list.Remove(this);
                                    }
                                }
                            }
                            field.SetValue(this, comboBox.SelectedItem);
                            foreach (var el in field.FieldType.GetFields())
                            {
                                if (el.FieldType.IsGenericType)
                                {
                                    IList list = (IList)el.GetValue(comboBox.SelectedItem);
                                    list.Add(this);
                                }
                            }
                        }
                    }
                    i += 2;
                }
                if (NeedToCreate)
                {
                    applicationDataContext.CallObjectCreatedEvent(applicationDataContext.Objects, this);
                }
                applicationDataContext.ComBoxObjectsRefresh();
                form.Close();
            };
            Button cancelButton = Controls.AddButton(form, y, ControlWidth, ControlWidth, ControlHeight, "Отмена");

            cancelButton.Click += (sender, e) =>
            {
                form.Close();
            };
            form.Show();
        }