Beispiel #1
0
        public static Control[] accountInfoCardPage(Form form, PersonAccountInfo ai)
        {
            List <Control> controls = new List <Control>();

            DataColumn[] fields      = PersonAccountInfo.dataColumns;
            object[]     data        = ai.getAsObjArr();
            int          startHeight = 50;
            int          startLeft   = 20;
            Size         defSize     = new Size(250, 80);

            for (int i = 0; i < fields.Length; i++)
            {
                Label lbl = new Label();
                lbl.Text = fields[i].ColumnName;
                lbl.Size = defSize;
                lbl.Top  = startHeight + (i * lbl.Size.Height) + 10;
                lbl.Left = startLeft;

                if (fields[i].DataType == typeof(DateTime))
                {
                    DateTimePicker dbox = new DateTimePicker();
                    dbox.Format = DateTimePickerFormat.Short;

                    try
                    {
                        dbox.Value = (DateTime)data[i];
                    }
                    catch (System.ArgumentOutOfRangeException)
                    {
                        dbox.Format       = DateTimePickerFormat.Custom;
                        dbox.CustomFormat = " ";
                        dbox.Validated   += new EventHandler(dBoxValidated);
                        dbox.MouseDown   += new MouseEventHandler(dBoxValidated);
                        void dBoxValidated(object sender, EventArgs e)
                        {
                            dbox.Format = DateTimePickerFormat.Short;
                        }
                    }
                    setControlBounds(dbox);
                    controls.Add(dbox);
                }
                else
                {
                    TextBox tbox = new TextBox();
                    if (i < data.Length && data[i] != null)
                    {
                        tbox.Text = data[i].ToString();
                    }
                    setControlBounds(tbox);
                    controls.Add(tbox);
                }
                void setControlBounds(Control control)
                {
                    control.Top  = lbl.Top;
                    control.Left = lbl.Left + lbl.Size.Width + 10;
                    control.Size = defSize;
                    control.Name = lbl.Text;

                    if (control is TextBox)
                    {
                        control.TextChanged += new EventHandler(ctrlTextChanged);
                    }
                    else if (control is ComboBox)
                    {
                        ComboBox cb = (ComboBox)control;
                        cb.SelectedValueChanged += new EventHandler(ctrlTextChanged);
                    }
                    else if (control is DateTimePicker)
                    {
                        DateTimePicker db = (DateTimePicker)control;
                        db.ValueChanged += new EventHandler(ctrlValidated);
                    }

                    void ctrlTextChanged(object sender, EventArgs e)
                    {
                        Control ctrl = (Control)sender;

                        ctrl.Validated += new EventHandler(ctrlValidated);
                    }

                    void ctrlValidated(object sender, EventArgs e)
                    {
                        Control ctrl = (Control)sender;

                        int fieldId = fields.ToList().IndexOf(fields.Where(x => x.ColumnName == ctrl.Name).First());

                        if (ctrl is DateTimePicker)
                        {
                            DateTime date;
                            if (DateTime.TryParse(ctrl.Text, out date))
                            {
                                data[fieldId] = date;
                            }
                        }
                        else
                        {
                            if (fields[fieldId].DataType == typeof(int))
                            {
                                int num;
                                if (int.TryParse(ctrl.Text, out num))
                                {
                                    if (fields[fieldId].Unique)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Der Primärschlüssel kann nicht geändert werden!", "Meldung");
                                        // person = DBCon.UpdateIdPerson(person, num);
                                        // data[fieldId] = num;
                                        // control.Parent.Dispose();
                                    }
                                    else
                                    {
                                        data[fieldId] = num;
                                    }
                                }
                            }
                            else
                            {
                                data[fieldId] = control.Text;
                            }
                        }
                        ai.setAsObjArr(data);
                        DBCon.UpsertPersonAccountInfo(ai);
                    }
                }

                controls.Add(lbl);
            }
            return(controls.ToArray());
        }
Beispiel #2
0
        public static Control[] memberCardPage(Form form1, Person person)
        {
            List <Control> controls = new List <Control>();

            DataColumn[] fields = Person.dataColumns;

            object[] data         = person.getAsObjArr();
            int      startHeight  = 50;
            int      startLeft    = 20;
            Size     defSize      = new Size(250, 80);
            int      fieldsPerCol = fields.Count() / 2;

            for (int i = 0; i < fields.Count(); i++)
            {
                Label lbl = new Label();
                lbl.Text = fields[i].ColumnName;
                lbl.Size = defSize;

                lbl.Top  = startHeight + (i % fieldsPerCol) * (lbl.Size.Height) + 20;
                lbl.Left = (i < fieldsPerCol) ? startLeft : (startLeft + defSize.Width * 2 + 50);

                if (fields[i].DataType == typeof(Person.type))
                {
                    ComboBox cbox = new ComboBox();
                    cbox.DropDownStyle = ComboBoxStyle.DropDownList;
                    foreach (var item in DBObject.GetEnumList <Person.type>())
                    {
                        cbox.Items.Add(item);
                    }
                    if (i < data.Length && data[i] != null)
                    {
                        cbox.SelectedItem = data[i];
                    }
                    else
                    {
                        cbox.Text = Person.type.Left.ToString();
                    }
                    setControlBounds(cbox);
                    controls.Add(cbox);
                }
                else if (fields[i].DataType == typeof(Person.paymentType))
                {
                    ComboBox cbox = new ComboBox();
                    cbox.DropDownStyle = ComboBoxStyle.DropDownList;

                    foreach (var item in DBObject.GetEnumList <Person.paymentType>())
                    {
                        cbox.Items.Add(item);
                    }
                    if (i < data.Length && data[i] != null)
                    {
                        cbox.SelectedItem = data[i];
                    }
                    else
                    {
                        cbox.Text = Person.paymentType.None.ToString();
                    }
                    setControlBounds(cbox);
                    controls.Add(cbox);

                    Button btn = new Button();
                    btn.Image  = Image.FromFile("./img/folder_contacts_smol.png");
                    btn.Height = cbox.Size.Height + 5;
                    btn.Width  = btn.Height + 5;
                    btn.Top    = cbox.Top;
                    btn.Left   = cbox.Left + cbox.Size.Width + 10;
                    btn.Click += new EventHandler(paiBtnClicked);
                    void paiBtnClicked(object sender, EventArgs e)
                    {
                        PersonAccountInfo pai = DBCon.getPAIforPerson(person);

                        if (pai == null)
                        {
                            pai            = new PersonAccountInfo();
                            pai.personId   = person.id;
                            pai.personName = person.lastName + ", " + person.firstName;
                            pai.mandateId  = person.id;
                            DBCon.UpsertPersonAccountInfo(pai);
                            AccountInfoCard aic = new AccountInfoCard(pai);
                            aic.ShowDialog();
                        }
                        else
                        {
                            AccountInfoCard aic = new AccountInfoCard(pai);
                            aic.ShowDialog();
                        }
                    }

                    controls.Add(btn);
                }
                else if (fields[i].DataType == typeof(DateTime))
                {
                    DateTimePicker dbox = new DateTimePicker();
                    dbox.Format = DateTimePickerFormat.Short;

                    try
                    {
                        dbox.Value = (DateTime)data[i];
                    }
                    catch (System.ArgumentOutOfRangeException)
                    {
                        // dbox.Enabled = false;
                        dbox.Format       = DateTimePickerFormat.Custom;
                        dbox.CustomFormat = " ";
                        dbox.Validated   += new EventHandler(dBoxValidated);
                        dbox.MouseDown   += new MouseEventHandler(dBoxValidated);

                        void dBoxValidated(object sender, EventArgs e)
                        {
                            dbox.Format = DateTimePickerFormat.Short;
                        }
                    }

                    setControlBounds(dbox);
                    controls.Add(dbox);
                }
                else
                {
                    TextBox tbox = new TextBox();
                    if (i < data.Length && data[i] != null)
                    {
                        tbox.Text = data[i].ToString();
                    }
                    setControlBounds(tbox);
                    controls.Add(tbox);
                }


                void setControlBounds(Control control)
                {
                    control.Top  = lbl.Top;
                    control.Left = lbl.Left + lbl.Size.Width + 10;
                    control.Size = defSize;
                    control.Name = lbl.Text;

                    if (control is TextBox)
                    {
                        control.TextChanged += new EventHandler(ctrlTextChanged);
                    }
                    else if ((control is ComboBox))
                    {
                        ComboBox cb = (ComboBox)control;
                        cb.SelectedValueChanged += new EventHandler(ctrlTextChanged);
                    }
                    else if (control is DateTimePicker)
                    {
                        DateTimePicker db = (DateTimePicker)control;
                        db.ValueChanged += new EventHandler(ctrlValidated);
                    }
                    void ctrlTextChanged(object sender, EventArgs e)
                    {
                        Control ctrl = (Control)sender;

                        ctrl.Validated += new EventHandler(ctrlValidated);
                    }

                    void ctrlValidated(object sender, EventArgs e)
                    {
                        Control ctrl    = (Control)sender;
                        int     fieldId = fields.ToList().IndexOf(fields.Where(x => x.ColumnName == ctrl.Name).First());

                        if (ctrl is ComboBox)
                        {
                            Enum.TryParse(fields[fieldId].DataType, ctrl.Text, out data[fieldId]);
                        }
                        else if (ctrl is DateTimePicker)
                        {
                            DateTime date;
                            if (DateTime.TryParse(ctrl.Text, out date))
                            {
                                data[fieldId] = date;
                            }
                        }
                        else
                        {
                            if (fields[fieldId].DataType == typeof(int))
                            {
                                int num;
                                if (int.TryParse(ctrl.Text, out num))
                                {
                                    if (fields[fieldId].Unique && person != null)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Nach einer Primärschlüsseländerung muss die aktuelle Seite geschlossen werden!", "Meldung");
                                        person        = DBCon.UpdateIdPerson(person, num);
                                        data[fieldId] = num;
                                        control.Parent.Dispose();
                                    }
                                    else
                                    {
                                        data[fieldId] = num;
                                    }
                                }
                            }
                            else
                            {
                                data[fieldId] = control.Text;
                            }
                        }
                        person.setAsObjArr(data);
                        DBCon.UpsertPerson(person);
                    }
                }

                controls.Add(lbl);
            }

            return(controls.ToArray());
        }