Exemple #1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool valid = validate();

            if (valid)
            {
                if (adding)
                {
                    // 1. Create a new Emp
                    CD cd = new CD();
                    cd.ID       = txtID.Text;
                    cd.Album    = txtAlbum.Text;
                    cd.Singer   = txtSinger.Text;
                    cd.Duration = long.Parse(txtDuration.Text);
                    cd.Songs    = txtSongs.Lines.OfType <string>().ToList <string>();
                    cd.Genre    = (CDGenre)cbGenre.SelectedIndex;
                    // 2. Add the new Emp to list
                    var mainForm = (DetailForm)this.Owner;
                    var list     = (BindingList <CD>)mainForm.dgCD.DataSource;
                    list.Add(cd);
                    mainForm.cdList.Add(cd);
                }
                else
                {
                    // 1. Update fields
                    oldCD.Album    = txtAlbum.Text;
                    oldCD.Singer   = txtSinger.Text;
                    oldCD.Duration = long.Parse(txtDuration.Text);
                    oldCD.Songs    = txtSongs.Text.Split('\n').ToList <string>();
                    oldCD.Genre    = (CDGenre)cbGenre.SelectedIndex;

                    var mainForm = (DetailForm)this.Owner;
                    mainForm.dgCD.Refresh();

                    // 2. Close Dialog
                    this.Close();
                }
            }
        }