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.
Our Complete Phonebook Application Will Look like this:
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:
//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;
}
}
}