Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Outlook Type Address Book in C#, LINQ, XML with Menu and ToolBar

, 28 Aug 2009
Outlook type Address Book in C#, LINQ, XML with Menu and ToolBar
AddressBook.zip
AddressBook
AddressBook
bin
Debug
AddressBook.exe
AddressBook.vshost.exe
AddressBook.vshost.exe.manifest
Properties
Settings.settings
Simon.ico
AddressBook.suo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace AddressBook
{
    public partial class AddressDetail : Form
    {
        List<Contacts> _contactList = new List<Contacts>();
        int currentlyShown = 0;
        IEnumerator etr;
        
        /// <summary>
        /// This constructor is called when you select any contact and want to see the properties.
        /// </summary>
        /// <param name="contactList"></param>
        /// <param name="index"></param>
        public AddressDetail(List<Contacts> contactList, int index)
        {
            InitializeComponent();
            currentlyShown = index;
            this._contactList = contactList;
            Text = _contactList[index].FName + " " + _contactList[index].LName + " " + "Properties";
            showData(_contactList[index]);
            etr = _contactList.GetEnumerator();
            
            Enable(false);
            
        }

        /// <summary>
        /// This constructor will be called when we want to add new contact to the address book.
        /// </summary>
        /// <param name="contactList"></param>
        public AddressDetail()
        {
            InitializeComponent();
           Text = "Add new Contact";
            btnAdd.Text = "&Save";
            btnDelete.Text = "&Cancel";
            Enable(true);

            //Here we shouid disable the next and previous button.
            btnNext.Enabled = false;
            btnPrevious.Enabled = false;
        }

        private void btnFirstRecord_Click(object sender, EventArgs e)
        {
            if (currentlyShown > 0 && _contactList.Count > 0)
            {
                currentlyShown = 0;
                showData(_contactList[currentlyShown]);
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currentlyShown > 0 && _contactList.Count > 0)
            {
                currentlyShown = currentlyShown - 1;
                showData(_contactList[currentlyShown]);
            }
            
            
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentlyShown < _contactList.Count-1 && _contactList.Count > 0)
            {
                currentlyShown = currentlyShown + 1;
                showData(_contactList[currentlyShown]);
            }
        }


        private void btnLast_Click(object sender, EventArgs e)
        {
            if (currentlyShown < _contactList.Count - 1 && _contactList.Count > 0)
            {
                currentlyShown = _contactList.Count-1;
                showData(_contactList[currentlyShown]);
            }
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            //check if the state is in modify state
            if (btnModify.Text == "&Change")
                return;


            if (btnAdd.Text == "&Add")
            {
                btnAdd.Text = "&Save";
                btnDelete.Text = "&Cancel";
                this.Enable(true);
                _txtFirstName.Focus();
                this.BlankBox();
            }
            else
            {
                if (!ValidateContactDetails())
                    return;

                if (IsContactExists())
                    return;


                btnAdd.Text = "&Add";
                btnDelete.Text = "&Delete";
                //new data row
                Contacts contact = new Contacts(_txtFirstName.Text, _txtLastName.Text, _txtEmail.Text,
                    _txtAddress1.Text, _txtAddress2.Text, _txtCity.Text, _txtState.Text, _txtCountry.Text,
                _txtZip.Text, _txtPriPhone.Text, _txtSecPhone.Text);
                           
                    
                    XMLParse.ContactList.Insert(currentlyShown, contact);

                    XMLParse.AddContact(contact);

                this.Enable(false);
                this.BlankBox();
                
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (btnDelete.Text == "&Delete")
            {
                this.Enable(false);
                 DialogResult dr = MessageBox.Show("This will delete the contact premanently. Are you sure?. ", "Delete Record Conformation", MessageBoxButtons.YesNo);
                    if (Convert.ToString(dr) == "Yes")
                    {

                        //foreach (Contacts cts in _contactList)
                        //{
                        //    if (cts.Email == _txtEmail.Text)
                        //    {
                        //        _contactList.Remove(cts);
                        //        XMLParse.deleteContact(cts.Email);
                        //        break;
                        //    }
                        //}

                        XMLParse.deleteContact(_txtEmail.Text);

                        if (currentlyShown > 0 && _contactList.Count > 0)
                        {
                            currentlyShown = currentlyShown - 1;
                            showData(_contactList[currentlyShown]);
                        }
                       
                    }
                }
           else
            {
                btnAdd.Text = "&Add";
                btnDelete.Text = "&Delete";
                btnModify.Text = "&Modify";
                this.Enable(false);
                this.BlankBox();
            }


        }



        private void btnModify_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "&Save")
                return;


            if (!(_txtFirstName.Text.Length == 0))
            {
                if (btnModify.Text == "&Modify")
                {

                    this.Enable(true);
                    _txtFirstName.Focus();
                    btnModify.Text = "&Change";
                    btnDelete.Text = "&Cancel";
                }
                else
                {
                    btnModify.Text = "&Modify";
                    btnDelete.Text = "&Delete";
                    Contacts cs = new Contacts(_txtFirstName.Text, _txtLastName.Text, _txtEmail.Text, _txtAddress1.Text, _txtAddress2.Text, _txtCity.Text, _txtState.Text, _txtCountry.Text, _txtZip.Text, _txtPriPhone.Text, _txtSecPhone.Text);
                   
                    for (int i=0; i<_contactList.Count; i++)
                    {
                        if (_contactList[i].Email == cs.Email)
                        {                             
                            _contactList.RemoveAt(i);
                            _contactList.Insert(i, cs);

                            //call the function to update your xml file as well
                            XMLParse.UpdateContact(cs);
                            
                        }
                    }
                 this.Enable(false);

                }
            }
            else
            {
                MessageBox.Show("U Must have to select person name from the list", "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void Enable(bool flag)
        {
            string str;
            foreach (Control ctrl in groupBox1.Controls)
            {
                str = Convert.ToString(ctrl.GetType());
                if (str == "System.Windows.Forms.TextBox")
                {
                    ctrl.Enabled = flag;
                }
            }
        }

        public void BlankBox()
        {
            string str;
            foreach (Control ctrl in this.Controls)
            {
                str = Convert.ToString(ctrl.GetType());
                if (str == "System.Windows.Forms.TextBox")
                {
                    ctrl.Text = "";
                }
            }
        }


        private void showData(Contacts contact)
        {
            Text = contact.FName + " " + contact.LName + " " + "Properties";
            _txtFirstName.Text = contact.FName;
            _txtLastName.Text = contact.LName;
            _txtEmail.Text = contact.Email;
            _txtAddress1.Text = contact.Address1;
            _txtAddress2.Text = contact.Address2;
            _txtCity.Text = contact.City;
            _txtState.Text = contact.State;
            _txtZip.Text = contact.Zip;
            _txtCountry.Text = contact.Country;
            _txtPriPhone.Text = contact.Phone1;
            _txtSecPhone.Text = contact.Phone2;
            

        }

        private bool ValidateContactDetails()
        {
            if (_txtFirstName.Text.Length <= 0)
            {
                MessageBox.Show("Enter a valid first name");
                return false;
            }
            if (_txtLastName.Text.Length <= 0)
            {
                MessageBox.Show("Enter a valid last name");
                return false;
            }
            if (_txtEmail.Text.Length <= 0)
            {
                MessageBox.Show("Enter a valid email address");
                return false;
            }
            return true;
        }
        private bool IsContactExists()
        {
              //IList<object> allContact = Marisa.DataAccessLayer.DataOperation.NHQuery("from Contacts s ");
            foreach (Contacts cts in this._contactList)
            {
                if (_txtEmail.Text == cts.Email)
                {
                    MessageBox.Show("A contact with this email already exist");
                    return true;
                }
            }
            return false;
        }

       

        public List<Contacts> upDatedList
        { 
            get { return _contactList;}          
        }

        private void AddressDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
        //    foreach (Contacts contact in _contactList)
        //        Marisa.DataAccessLayer.DataOperation.NHDelete(contact);
        //    foreach (Contacts contact in _contactList)
        //        Marisa.DataAccessLayer.DataOperation.NHSave(contact);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nadeem Alvi
Software Developer Shell Oil
United States United States
I am working as a Senior Software Developer with Shell Oil,Houston Texas USA.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 28 Aug 2009
Article Copyright 2009 by Nadeem Alvi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid