Introduction: How to Create Simple Phone Book Application C#

Hi, I am Luke, it's my first Instructable. I want to show you how to create a simple phone book application in Microsoft Visual Studio using C#. It's good to have some basic knowledge of programming before making this project.

Let's get started.
We need Microsoft Visual Studio, it's free for students, you can get Professional Edition from MSDNAA. Search google for more information.

This is how our application will look like:

Step 1: Creating New Project in Microsoft Visual Studio

Start Microsoft Visual Studio, and create new Project, choose Windows Forms Application remeber project type is Visual C#. You can name your project whatever you want and change location for the project if nessesary.

Step 2: Adding Everything to Form

Now, it's a empty Form. Let's add to it some components from toolbox as seen on the picture. They are: DataGridView, SaveFileDioalog, OpenFileDialog and menuStrip

Step 3: Adding Columns

After adding DataGridView, We have empty space, click right mouse button on it and select edit Columns.

Step 4: Before Writing a Code

Make sure your form looks like this and DataGridView is named "GRID". You can set it in properies window

Step 5: Writing a Code

Click two times on each element of your menu, to create events, each time a window with a code will be showed, so go back and to it with all (Save, Open, Close)
That what we need in code :

private void SaveToolStripMenuItem_Click(object sender, EventArgs e)


private void OpenToolStripMenuItem_Click(object sender, EventArgs e)


private void CloseToolStripMenuItem_Click(object sender, EventArgs e)


Step 6: Code Code Code....

Here is a full code of our application with comments after "//"cases:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO; //added
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary; //added
using System.Runtime.Serialization; //added
namespace testowa // this my name of project

public partial class Form1 : Form
public Form1()

[Serializable] // It allow our class to be saved in file
public class data // Our class for data
public string name;
public string surname;
public string city;
public string number;


private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
SaveFileDialog saveFileDialog1 = new SaveFileDialog(); //Creating a file save dialog

saveFileDialog1.RestoreDirectory = true;
//read and filter the raw data
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
BinaryFormatter formatter = new BinaryFormatter();
FileStream output = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write);
int n = GRID.RowCount;
data[] Person = new data[n - 1]; //We have as many records as many rows, rows are added automaticly so we have always one row more than we need, so n is a number of rows -1 empty row
for (int i = 0; i < n - 1; i++)

Person[i] = new data();
//GRID has two numbers in"[]" first numer is an index of column, second is a an idnex of row', indexing always starts from 0'
Person[i].name = GRID[0, i].Value.ToString();
Person[i].surname = GRID[1, i].Value.ToString();
Person[i].city = GRID[2, i].Value.ToString();
Person[i].number = GRID[3, i].Value.ToString();


formatter.Serialize(output, Person);



private void OpenToolStripMenuItem_Click(object sender, EventArgs e) // Reading a File and adding data to GRID
openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
BinaryFormatter reader = new BinaryFormatter();
FileStream input = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
data[] Person = (data[])reader.Deserialize(input);
for (int i = 0; i < Person.Length; i++)
GRID[0, i].Value = Person[i].name;
GRID[1, i].Value = Person[i].surname;
GRID[2, i].Value = Person[i].city;
GRID[3, i].Value = Person[i].number;



private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
Close(); // closing an app

Step 7: It's Done. Test It

Click on menu Debug in Visual Studio than start Debugging it should works. Try to test the app. I am sure you will find some bugs ,our applications is very simple without any securities while saving, opening files, it's only demonstration how to do a bigger useful application. You can improve it, make your own version! Add some new features, change whatever you want.  Good luck!