Click here to Skip to main content
15,894,646 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.70/5 (8 votes)
28 Aug 2009CPOL4 min read 41K   1.6K   31  
Outlook type Address Book in C#, LINQ, XML with Menu and ToolBar
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml;
using System.Linq;
using System.Xml.Linq;

namespace AddressBook
{
    public partial class AddressBookForm : Form
    {
        #region Fields
        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem mnuFile;
        private System.Windows.Forms.MenuItem mnuView;
        private System.Windows.Forms.MenuItem mnuFileExit;
        private System.Windows.Forms.MenuItem mnuFileAdd;
        private System.Windows.Forms.MenuItem mnuFileProperties;
        private System.Windows.Forms.MenuItem mnuViewToolbar;
        #endregion
        public AddressBookForm()
        {
            InitializeComponent();
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.mnuFile = new System.Windows.Forms.MenuItem();
            this.mnuView = new System.Windows.Forms.MenuItem();
            this.mnuFileExit = new System.Windows.Forms.MenuItem();
            this.mnuFileAdd = new System.Windows.Forms.MenuItem();
            this.mnuFileProperties = new System.Windows.Forms.MenuItem();
            this.mnuViewToolbar = new System.Windows.Forms.MenuItem();

            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.mnuView, this.mnuFile});
            // 
            // mnuFile
            // 
            this.mnuFile.Index = 0;
            this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.mnuFileAdd,this.mnuFileProperties, this.mnuFileExit });
            this.mnuFile.Text = "&File";
            // 
            this.mnuFileAdd.Index = 0;
            this.mnuFileAdd.Text = "A&dd New";
            this.mnuFileAdd.Click += new System.EventHandler(this.btnAdd_Click);

            this.mnuFileProperties.Index = 1;
            this.mnuFileProperties.Text = "S&how properties";
            this.mnuFileProperties.Click += new System.EventHandler(this._dgAddressBook_DoubleClick);
            // mnuFileExit
            // 
            this.mnuFileExit.Index = 2;
            this.mnuFileExit.Text = "E&xit";
            this.mnuFileExit.Click += new EventHandler(this.btnExit_Click);

             this.mnuView.Index = 1;
             this.mnuView.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuViewToolbar});
             this.mnuView.Text = "&View";

             this.mnuViewToolbar.Index = 0;
             this.mnuViewToolbar.Text = "V&iew ToolBar";
             this.mnuViewToolbar.Checked = true;
             this.mnuViewToolbar.Click += new System.EventHandler(this.ViewToolbar_Click);
             this.Menu = this.mainMenu1;
             ToolTip toolTip1 = new ToolTip();
             toolTip1.SetToolTip(this.btnAdd, "Add New");
             toolTip1.SetToolTip(this.btnDelete, "Delete");
             toolTip1.SetToolTip(this.txtSearch, "Type Name to Search");

        }


        private void AddressBookForm_Load(object sender, EventArgs e)
        {            
            XMLParse.getAllAddress();
            populateData(XMLParse.ContactList);
        }

        private void populateData(List<Contacts> ContactList)
        {            

            _dgAddressBook.BeginUpdate();
            _dgAddressBook.Items.Clear();

            foreach (Contacts cts in ContactList)
            {
                ListViewItem item = _dgAddressBook.Items.Add(cts.FName);
                item.SubItems.Add(cts.LName);
                item.SubItems.Add(cts.Phone1);
                item.SubItems.Add(cts.Phone2);
                item.SubItems.Add(cts.Address1);
                item.SubItems.Add(cts.Address2);
                item.SubItems.Add(cts.City);
                item.SubItems.Add(cts.State);
                item.SubItems.Add(cts.Zip);
                item.SubItems.Add(cts.Country);
                item.SubItems.Add(cts.Email);
            }

            _dgAddressBook.EndUpdate();
                                               
        }

        private void _dgAddressBook_DoubleClick(object sender, EventArgs e)
        {
            if (_dgAddressBook.SelectedItems.Count <= 0)
                return;

            int index = _dgAddressBook.SelectedItems[0].Index;
            AddressDetail ad = new AddressDetail(XMLParse.ContactList, index);
            ad.ShowDialog();
            populateData(XMLParse.ContactList);
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddressDetail ad = new AddressDetail();
            ad.ShowDialog();
            populateData(XMLParse.ContactList);
        }

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

        private void ViewToolbar_Click(object sender, EventArgs e)
        {
            if (grbToolbar.Visible)
            {
                grbToolbar.Visible = false;
                this.mnuViewToolbar.Checked = false;
            }
            else
            {
                grbToolbar.Visible = true;
                this.mnuViewToolbar.Checked = true;
            }
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            //If enter key was pressed then search for that key in database and populate the list.
            if (e.KeyChar == (char)13)
            {
                
                string searhName = txtSearch.Text;
                List<Contacts> searchedContactList = new List<Contacts>();

                foreach (Contacts contact in XMLParse.ContactList)
                {
                    if(contact.FName.IndexOf(searhName,StringComparison.CurrentCultureIgnoreCase) != -1)
                        searchedContactList.Add(contact);
                }

                populateData(searchedContactList);
                
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
          //Check if any row was selected.
            if (_dgAddressBook.SelectedItems.Count <= 0)
                return;
            
            DialogResult dr = MessageBox.Show("This will delete the contact premanently. Are you sure?. ", "Delete Record Conformation", MessageBoxButtons.YesNo);
            if (Convert.ToString(dr) == "Yes")
            {
                //find the email as that is the key
                string email = _dgAddressBook.SelectedItems[0].SubItems[10].Text;
                XMLParse.deleteContact(email);
                populateData(XMLParse.ContactList);
            }
            

        }






    }
}

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)


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

Comments and Discussions