Topic : C# Project - Phone Book in C# Programming Language:

Phone Book Real Application in C#:

In this Tutorial, we will Create a Real-World Phone Book Application in C# Programming Language. In This Application, we will Include the Following features.

  • » We Can Add Contact Number including Name, Phone Number, Email ID and Address:
    » We can Update the Record:
    » We can Delete the Record:
    » We can Search the Record:



Our Complete Phonebook Application Will Look like this:

PhoneBook Project : infobrother

  • Open Your Visual Studio, create New Project (File > New > Project) and Select Visual C# > Windows > Windows Forms Application type the Project Name and Create it. Drag and Drop the control to form using toolbox, as shown in above picture: Click the button One by One and write the given Code: Watch the Given video for complete tutorial:



Code for New Button:

//Code for "New" Button: 

private void btnNew_Click(object sender, EventArgs e)
 {
   try
   {
      panel1.Enabled = true;
      //Add a New Row:
      App.PhoneBook.AddPhoneBookRow(App.PhoneBook.NewPhoneBookRow());
      phoneBookBindingSource.MoveLast();
      txtName.Focus();
   }
  catch (Exception ex)
   {
      MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
      App.PhoneBook.RejectChanges();
   }
 }

Panel:

Panel is a container used to group collections of control. We can use it to group collections of controls such as we can group some textbox and button controls. if the panel control's Enabled property is set to false, the controls contained within the panel will also be disables.



Code for Edit Button:

////Code for "Edit" button:
private void btnEdit_Click(object sender, EventArgs e)
{
   panel1.Enabled = true;
   txtPhone.Focus();
}


Code for Cancel Button:

//Code for "Cancel"  Button:
 private void btnCancel_Click(object sender, EventArgs e)
 {
    phoneBookBindingSource.ResetBindings(false);
    panel1.Enabled = false;
 }


Code for Save Button:

//Code for "Save"  button:
private void btnSave_Click(object sender, EventArgs e)
{
  try
  {
    //End Edit, Save Data To file:
    phoneBookBindingSource.EndEdit();
    App.PhoneBook.AcceptChanges();
    App.PhoneBook.WriteXml(string.Format("{0}//data.dat", Application.StartupPath));
    panel1.Enabled = false;

     MessageBox.Show("Number Store Successfully:");
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK,  MessageBoxIcon.Error);
      App.PhoneBook.RejectChanges();
    }
}


Code to Create Instance of DataSet:

In Order to store our Data, we need to create a Dataset. to create the dataSet, follow the given Steps:


  • » From Solution Explorer tab, right click on PhoneBook Project and choose Add > New Item:
    » From Visual C# Items Tab, select Data > DataSet, give it valid name and Click ok:
    » Create the Table, as shown in the picture Below:
    » Create an Instance of DataSet through given code:
    » Follow Video Tutorial for More Information:


PhoneBook Project : infobrother
//Use Singleton Pattern to Create an Instance.
static PhoneData db;
protected static PhoneData App
{
   get
    {
       if (db == null)
         {
             db = new PhoneData();
         }
        return db;
     }
}

Singleton Pattern:

The Singleton Pattern is one of the best-known patterns in software Engineering. A singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance.



Code For Form:

Now we Need to add some code for form, so it can read Xml file for phonebook record:


 //Code for "Form".
 private void Form1_Load(object sender, EventArgs e)
 {
     string filename = string.Format("{0}//data.dat", Application.StartupPath);
     if (File.Exists(filename))
      {
          App.PhoneBook.ReadXml(filename);
      }
     phoneBookBindingSource.DataSource = App.PhoneBook;
     panel1.Enabled = false;
 }


Code for DataGridView:

DataGridView help to displays data in a Customizable grid. The DataGridView control makes it easy to define the basic appearance of cells and the display formatting of cell values.


//Code for "DataGridView". 
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Delete)
    {
        if (MessageBox.Show("Are You sure that you want to Delete this Record?", "Message",
           MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                phoneBookBindingSource.RemoveCurrent();
            }
    }
}


Code For Search Box:

 //code for "Search box";
 private void txtSearch_KeyPress_1(object sender, KeyPressEventArgs e)
 {
    if (e.KeyChar == (char)13) //enter Key:
     {
        if (!string.IsNullOrEmpty(txtSearch.Text))
          {

            //we can use linq to Query data:
             var query = from o in App.PhoneBook
             where o.PhoneNo == txtSearch.Text 
                             ||    o.FullName.ToLowerInvariant().Contains(txtSearch.Text.ToLowerInvariant())
                             ||    o.Email.ToLowerInvariant() == txtSearch.Text.ToLowerInvariant()
            select o;
            dataGridView1.DataSource = query.ToList();
          }
       else
         {
            dataGridView1.DataSource = phoneBookBindingSource;
         }
     }
  }




















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement