コード例 #1
0
        private void InitialiseChamp()
        {
            if (template == null)
            {
                return;
            }

            Control ctrl = null;
            int     x = 20, y = 20;

            foreach (Champ c in template.getChamps())
            {
                c.isValide = true;

                Label l = new Label();
                l.Text      = c.getLibelle();
                l.AutoSize  = true;
                l.BackColor = Color.Transparent;
                l.Location  = new Point(x, y);
                l.Parent    = this.panel1;

                Donnee d = null;

                if (contact != null)
                {
                    d = contact.getDonnees().Where(dd => dd.getIdchamp() == c.getIdchamp()).FirstOrDefault();
                }


                if (c.getDatatype().getLibelle() == "DATE")
                {
                    ctrl = new DateTimePicker();
                    if (d != null && !string.IsNullOrEmpty(d.getValeur()))
                    {
                        ((DateTimePicker)ctrl).Value = DateTime.Parse(d.getValeur());
                    }
                }
                else if (c.getPreselectionsize() > 0)
                {
                    ctrl = new ComboBox();

                    foreach (string s in c.getPreselection())
                    {
                        ((ComboBox)ctrl).Items.Add(s);
                        if (d != null)
                        {
                            ((ComboBox)ctrl).SelectedItem = d.getValeur();
                        }
                    }
                }
                else
                {
                    ctrl           = new TextBox();
                    ctrl.KeyPress += Ctrl_KeyPress;

                    if (!String.IsNullOrEmpty(c.getDatatype().getRegex()))
                    {
                        ctrl.Leave     += Ctrl_Leave;
                        ctrl.LostFocus += Ctrl_Leave;
                    }

                    if (d != null)
                    {
                        ((TextBox)ctrl).Text = d.getValeur();
                    }
                }

                ctrl.Tag      = c;
                ctrl.Width    = 200;
                ctrl.Location = new Point(120, y);
                ctrl.Parent   = this.panel1;

                y = y + ctrl.Height + 10;
            }

            Button btnAjouter = new Button();

            btnAjouter.Name     = "BtnAjouter";
            btnAjouter.Text     = (contact == null? "Ajouter":"Modifier");
            btnAjouter.Width    = 100;
            btnAjouter.Location = new Point(x, y);
            btnAjouter.Parent   = this.panel1;
            btnAjouter.Click   += BtnAjouter_Click;

            Button btnQuitter = new Button();

            btnQuitter.Text     = "Quitter";
            btnQuitter.Width    = 100;
            btnQuitter.Location = new Point(btnAjouter.Width + 40, y);
            btnQuitter.Parent   = this.panel1;
            btnQuitter.Click   += BtnQuitter_Click;

            if (ctrl != null)
            {
                x = ctrl.Location.X + ctrl.Width + 40;
                y = toolStrip1.Location.Y - 20;
            }

            this.panel1.Size = new Size(x, y);
        }