FORM 1:
The first form contains all the buttons for the Phonebook application.

The code for FORM 1 is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PhoneProgram
{
public partial class frmAddressBook : Form
{
public static int iCount = 0;
public static string[,] sName = { { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" } };
public static string sFriendModify = "";
frmAddForm addForm = new frmAddForm();
Prompt promptForm = new Prompt();
public frmAddressBook()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
addForm.ShowDialog();
cmbName.Items.Clear();
//add new items
for (int i = 0; i < 10; i++)
{
if ((sName[i, 0] != "") || (sName[i, 0] != null))
{
//cmbName.Items.Add(sName[i,0]);
cmbName.Items.Add(sName[i, 0].ToString());
}
}
}
private void btnFind_Click(object sender, EventArgs e)
{
if (cmbName.Text != "")
{
for (int i = 0; i < 10; i++)
{
if (sName[i, 0] == cmbName.Text)
{
MessageBox.Show(sName[i, 0].ToString() + " has phone number " + sName[i, 1].ToString(), sName[i, 0].ToString() + " Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
//reached end of array for search
{
MessageBox.Show(cmbName.Text.ToString() + " was not found. ", cmbName.Text.ToString() + " Not Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("No friend selected", "Select a Friend to find", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnCount_Click(object sender, EventArgs e)
{
int iFriends = 0;
for (int i = 0; i < 10; i++)
{
if (sName[i, 0] != "")//empty
{
++iFriends;
}
}
MessageBox.Show("You have " + iFriends + " friends in your Address Book.", iFriends.ToString() + " Friends Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnModify_Click(object sender, EventArgs e)
{
if (cmbName.Text == "")
{
MessageBox.Show("No friend selected", "No friend selected to modify", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
//modify
sFriendModify = cmbName.Text;
promptForm.ShowDialog();
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (cmbName.Text == "")
{
MessageBox.Show("No friend selected", "No friend selected to modify", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
for (int i = 0; i < 10; i++)
{
if (cmbName.Text == sName[i, 0])
{
cmbName.Items.Remove(sName[i, 0].ToString());
//perform delete
sName[i, 0] = "";
sName[i, 1] = "";
MessageBox.Show("Contact Details deleted", "Contact Details deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
}
}
}
}
FORM 2:
The second form will enable the user to enter name and phone number in the Phonebook.

Code is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PhoneProgram
{
public partial class frmAddForm : Form
{
public frmAddForm()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
frmAddForm.ActiveForm.Close();
}
private void btnConfirm_Click(object sender, EventArgs e)
{
if (fnValidate() == 0)
{
for (int i = 0; i < 10; i++)
{
if ((frmAddressBook.sName[i, 0] == "") || (frmAddressBook.sName[i, 0] == null)) //empty element
{
frmAddressBook.sName[i, 0] = txtName.Text.Trim();
frmAddressBook.sName[i, 1] = txtPhone.Text.Trim();
txtName.Text = "";
txtPhone.Text = "";
MessageBox.Show("Friend added", "AddressBook Updated", MessageBoxButtons.OK, MessageBoxIcon.Information);
//update combobox
return;
}
}
/* Array Full */
MessageBox.Show("Address Book is Full", "Address Book Full", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/***********************Function Lists *************************/
private int fnValidate()
{
if (txtName.Text == "")
{
MessageBox.Show("Enter a Name", "Name missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtName.Focus();
return -1;
}
if (txtPhone.Text != "")
{
try
{
int iPhoneNum = int.Parse(txtPhone.Text);
if ((txtPhone.Text.Length > 10) || (txtPhone.Text.Length < 8))
{
MessageBox.Show("Phone Number is not valid", "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPhone.SelectAll();
return -1;
}
}
catch
{
MessageBox.Show("Phone Number is not valid", "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPhone.SelectAll();
return -1;
}
return 0; //success
}
else
{
MessageBox.Show("Enter a Phone Number", "Enter Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPhone.Focus();
return -1;
}
}
}
}
FORM 3:
The third and last form enables the user to enter a phone number for the purpose of modifying an existing contact's phone number.

Code is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PhoneProgram
{
public partial class Prompt : Form
{
public Prompt()
{
InitializeComponent();
}
private void btnModify_Click(object sender, EventArgs e)
{
if (txtPhoneNum.Text != "")
{
try
{
int iPhoneNum = int.Parse(txtPhoneNum.Text);
if ((txtPhoneNum.Text.Length > 10) || (txtPhoneNum.Text.Length < 8))
{
MessageBox.Show("Phone Number is not valid", "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPhoneNum.SelectAll();
return;
}
}
catch
{
MessageBox.Show("Phone Number is not valid", "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPhoneNum.SelectAll();
return;
}
//modify phone number
for (int i = 0; i < 10 ; i++)
{
if (frmAddressBook.sName[i, 0] == frmAddressBook.sFriendModify)
{
frmAddressBook.sName[i, 1] = txtPhoneNum.ToString().Trim();
MessageBox.Show(frmAddressBook.sName[i, 0].ToString() + ", Phone Number is updated to " + txtPhoneNum.Text.ToString(), "Phone Number Updated", MessageBoxButtons.OK, MessageBoxIcon.Information);
Prompt.ActiveForm.Close();
}
}
}
else
{
MessageBox.Show("Enter a Phone Number", "Enter Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPhoneNum.Focus();
return;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
Prompt.ActiveForm.Close();
}
private void Prompt_Load(object sender, EventArgs e)
{
lblFriendName.Text = frmAddressBook.sFriendModify.ToString();
}
}
}
No comments:
Post a Comment