loading
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()
{
InitializeComponent();
}

[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)
{
GRID.EndEdit();
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);

output.Close();
}

}

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);
GRID.Rows.Clear();
for (int i = 0; i < Person.Length; i++)
{
GRID.Rows.Add();
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!



<p>the code works flawlessly!</p>
<p>one error (GRID) its coing all the time</p>
Error 2 'Phonebook.Form1.Dispose(bool)': no suitable method found to override<br><br><br>HELP ME!
<p>EndEdit() is your own Created Method right ??</p>
<p>sign up and earn money www.jobboy.com</p>
Hi Man, welcome to Instructables. This is something I wouldn't do, but it's well done (and the English is fine). L
<p>Keep getting this error (GRID) not found in context, can anyone help</p>
change your datagridview1 name with GRID
<p>good</p>
<p> When i click in save button on menustrip the dialog window opens and where I must save the file? Can anybody help me?</p>
<p>A czy to samo, tylko po Polsku, jest gdzieś dostępne?</p>
hi luke!<br/>i guess this would be more useful if it has search, edit buttons and textbox or label to display the search item..<sup>_</sup><br/>coz it would be easier to search for the person your looking for if you listed 50 or more names.<br/><br/>hope you add em. <sup></sup>,<br/>
Yes you are right. I'll do it in this week I think so :)
kindly pm me if your done with the additional modifications. thanks!
you can now change the apps name from "simple phonebook" to "simple phonebook: with search and edit". that would better eh!?

About This Instructable

168,907views

17favorites

License:

More by lucash:VOIP in your cell phone via wifi and 3G How to create simple phone book application C# 
Add instructable to: