Ejemplo n.º 1
0
 private void btnSimpan_Click(object sender, EventArgs e)
 {
     // validasi
     if (this.txtNama.Text.Trim() == "") // jika isian nama kosong
     {
         MessageBox.Show("Sorry, nama wajib isi ...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
         this.txtNama.Focus();
     }
     else if (this.txtAlamat.Text.Trim() == "") // jika alamat kosong
     {
         MessageBox.Show("Sorry, alamat wajib isi ...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
         this.txtAlamat.Focus();
     }
     else if (this.txtKota.Text.Trim() == "") // jika kota kosong
     {
         MessageBox.Show("Sorry, kota wajib isi ...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
         this.txtKota.Focus();
     }
     else if (this.txtNoHp.Text.Trim() == "") // jika nohp kosong
     {
         MessageBox.Show("Sorry, no. hp wajib isi ...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
         this.txtNoHp.Focus();
     }
     else if (this.txtEmail.Text.Trim() == "") // jika email kosong
     {
         MessageBox.Show("Sorry, email wajib isi ...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
         this.txtEmail.Focus();
     }
     else
     {
         AddressBookController adrCon;
         adrCon = new AddressBookController();
         adrCon.TambahData(true, txt);
     }
 }
        private void btnTambah_Click(object sender, EventArgs e)
        {
            adrCon = new AddressBookController();
            FrmTambahData form = new FrmTambahData(true);

            if (form.Run(form))
            {
                adrCon.LoadData(dgvData);
            }
        }
        private void btnFilter_Click(object sender, EventArgs e)
        {
            adrCon = new AddressBookController();

            if (this.txtNama.Text.Trim() != "" || this.txtAlamat.Text.Trim() != "" || this.txtKota.Text.Trim() != "" || this.txtNoHp.Text.Trim() != "" || this.txtTglLahir.Text.Trim() != "" || this.txtEmail.Text.Trim() != "")
            {
                try
                {
                    this.dgvData.Rows.Clear();
                    string[] fileContent = File.ReadAllLines("addressbook.csv");
                    foreach (string line in fileContent)
                    {
                        bool     benar   = false;
                        string[] arrItem = line.Split(';');
                        if (!benar && this.txtNama.Text.Trim() != "" && arrItem[0].ToLower().Contains(this.txtNama.Text.ToLower()))
                        {
                            benar = true;
                        }
                        if (!benar && this.txtAlamat.Text.Trim() != "" && arrItem[1].ToLower().Contains(this.txtAlamat.Text.ToLower()))
                        {
                            benar = true;
                        }
                        if (!benar && this.txtKota.Text.Trim() != "" && arrItem[2].ToLower().Contains(this.txtKota.Text.ToLower()))
                        {
                            benar = true;
                        }
                        if (!benar && this.txtNoHp.Text.Trim() != "" && arrItem[3].ToLower().Contains(this.txtNoHp.Text.ToLower()))
                        {
                            benar = true;
                        }
                        if (!benar && this.txtEmail.Text.Trim() != "" && arrItem[5].ToLower().Contains(this.txtEmail.Text.ToLower()))
                        {
                            benar = true;
                        }
                        if (!benar && this.txtTglLahir.Text.Trim() != "")
                        {
                            DateTime tglDari, tglSampai;
                            if (this.txtTglLahir.Text.Trim().Contains("-"))
                            {
                                string[] arrTanggal = this.txtTglLahir.Text.Split('-');
                                if (!DateTime.TryParse(arrTanggal[0], out tglDari))
                                {
                                    throw new Exception("Sorry, kriteria tanggal lahir tidak valid ...");
                                }
                                if (!DateTime.TryParse(arrTanggal[1], out tglSampai))
                                {
                                    throw new Exception("Sorry, kriteria tanggal lahir tidak valid ...");
                                }
                            }
                            else
                            {
                                if (!DateTime.TryParse(this.txtTglLahir.Text, out tglDari))
                                {
                                    throw new Exception("Sorry, kriteria tanggal lahir tidak valid ...");
                                }
                                tglSampai = tglDari;
                            }
                            DateTime tglLahir = Convert.ToDateTime(arrItem[4]);
                            if (tglLahir.Date >= tglDari.Date && tglLahir.Date <= tglSampai.Date)
                            {
                                benar = true;
                            }
                        }
                        if (benar)
                        {
                            this.dgvData.Rows.Add(new string[] { arrItem[0], arrItem[1], arrItem[2], arrItem[3], arrItem[4], arrItem[5] });
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                adrCon.LoadData(dgvData);
            }
        }
 private void btnHapus_Click(object sender, EventArgs e)
 {
     adrCon = new AddressBookController();
     adrCon.HapusData(dgvData, selectedRows, selectedRowsCount);
     adrCon.LoadData(dgvData);
 }
 private void btnEdit_Click(object sender, EventArgs e)
 {
     adrCon = new AddressBookController();
     adrCon.EditData(dgvData);
 }
 private void FrmAddressBook_Load(object sender, EventArgs e)
 {
     adrCon = new AddressBookController();
     adrCon.LoadData(dgvData);
 }