I have been trying this for days now and I am not even sure if I am going about this right. I've got errors errors errors everywhere. What I am trying to do is write a windows app that maintains a contact book that can hold up to 20 entries. I can store my data with arrys or classes.
Each entry has Last and first name, street, city, state and zip.
-I need menu functionality for Open and Save the file.
-Display the whole address book (just names in alpha order)
-Display all info about an individual that a user selects. (i used buttons for the two display options.)
-Add address book entry (btnAdd)
-Delete Address Book entry (btnDelete)
Now the only time they should be using the text file is:
1. when the user first chooses to open an address book (data is read into the classes. (I'm assuming I use StreamReader)
2. when the user chooses to save an address book file or close the program (data is written from the classes. (streamWriter??)
I am extremely confused about the whole thing and I'm not even sure I am even going about this correctly.
Can anyone help? Any help will be greatly appreciated.
This is what I have:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace ProjectTen
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmAddress : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.MainMenu mainMenu2;
private System.Windows.Forms.MenuItem menuFile;
private System.Windows.Forms.MenuItem menuOpen;
private System.Windows.Forms.MenuItem menuSave;
private System.Windows.Forms.Button btnDisplayContact;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnDisplayAll;
private StreamReader iFile;
private StreamWriter oFile;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.OpenFileDialog openFD;
private System.Windows.Forms.SaveFileDialog saveFD;
private System.Windows.Forms.ListBox lstBoxDisplay;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmAddress()
{
}
private void btnDisplayContact_Click(object sender, System.EventArgs e)
{
int displayContact = lstBoxDisplay.Items.Count;
string[] lastName = new string[displayContact];
for (int l = 0; l < displayContact; l++)
{
lastName[l] = Convert.ToString(lstBoxDisplay.Items[l]);
}
Array.Sort(lastName);
lstBoxDisplay.Items.Clear();
lstBoxDisplay.Items.AddRange(lastName);
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
this.lstBoxDisplay.Items.Add();
}
private void btnDisplayAll_Click(object sender, System.EventArgs e)
{
MessageBox.Show(firstName + "\n" + lastName + "\n" + address + "\n" +
city + "\n" + state + "\n" + zipCode);
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
if (lastName.Items.Count < 1)
{
MessageBox.Show("There are no items to delete");
}
else if (lstBoxDisplay.Text == "")
{
MessageBox.Show("Please select an item to delete");
}
else
{
DialogResult button = MessageBox.Show(
"Do you want to perform this action?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2 );
if (button == DialogResult.Yes)
{
lstBoxDisplay.Items.RemoveAt(lstBoxDisplay.SelectedIndex);
}
}
}
private void menuSave_Click(object sender, System.EventArgs e)
{
string Saved_File = "Address.txt";
saveFD.InitialDirectory = "";
saveFD.Title = "Save";
saveFD.FileName = "Address.txt";
saveFD.Filter = "Text Files|*.txt";
if (saveFD.ShowDialog() != DialogResult.Cancel)
{
Saved_File = saveFD.Address.txt;
}
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Application.Run(new frmAddress());
}
private void InitializeComponent()
{
this.mainMenu2 = new System.Windows.Forms.MainMenu();
this.menuFile = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuOpen = new System.Windows.Forms.MenuItem();
this.menuSave = new System.Windows.Forms.MenuItem();
this.btnDisplayContact = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.btnDisplayAll = new System.Windows.Forms.Button();
this.openFD = new System.Windows.Forms.OpenFileDialog();
this.saveFD = new System.Windows.Forms.SaveFileDialog();
this.lstBoxDisplay = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// mainMenu2
//
this.mainMenu2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFile});
//
// menuFile
//
this.menuFile.Index = 0;
this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuOpen,
this.menuSave});
this.menuFile.Text = "File";
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "-";
//
// menuOpen
//
this.menuOpen.Index = 1;
this.menuOpen.Text = "Open";
this.menuOpen.Click += new System.EventHandler(this.menuOpen_Click);
//
// menuSave
//
this.menuSave.Index = 2;
this.menuSave.Text = "Save";
this.menuSave.Click += new System.EventHandler(this.menuSave_Click);
//
// btnDisplayContact
//
this.btnDisplayContact.Location = new System.Drawing.Point(165, 205);
this.btnDisplayContact.Name = "btnDisplayContact";
this.btnDisplayContact.Size = new System.Drawing.Size(100, 23);
this.btnDisplayContact.TabIndex = 0;
this.btnDisplayContact.Text = "Display Contact";
this.btnDisplayContact.Click += new System.EventHandler(this.btnDisplayContact_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(20, 165);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(100, 23);
this.btnAdd.TabIndex = 1;
this.btnAdd.Text = "Add Entry";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnDelete
//
this.btnDelete.Location = new System.Drawing.Point(165, 165);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(100, 23);
this.btnDelete.TabIndex = 2;
this.btnDelete.Text = "Delete Entry";
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnDisplayAll
//
this.btnDisplayAll.Location = new System.Drawing.Point(20, 205);
this.btnDisplayAll.Name = "btnDisplayAll";
this.btnDisplayAll.Size = new System.Drawing.Size(100, 23);
this.btnDisplayAll.TabIndex = 3;
this.btnDisplayAll.Text = "Display All";
this.btnDisplayAll.Click += new System.EventHandler(this.btnDisplayAll_Click);
//
// lstBoxDisplay
//
this.lstBoxDisplay.Location = new System.Drawing.Point(20, 20);
this.lstBoxDisplay.Name = "lstBoxDisplay";
this.lstBoxDisplay.Size = new System.Drawing.Size(240, 121);
this.lstBoxDisplay.TabIndex = 4;
//
// frmAddress
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.lstBoxDisplay);
this.Controls.Add(this.btnDisplayAll);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnDisplayContact);
this.Menu = this.mainMenu2;
this.Name = "frmAddress";
this.Text = "Address Book";
this.Load += new System.EventHandler(this.frmAddress_Load_1);
this.ResumeLayout(false);
}
private void frmAddress_Load_1(object sender, System.EventArgs e)
{
}
private void menuOpen_Click(object sender, System.EventArgs e)
{
String[] firstName = {"Julie", "Tony", "Frederick", "Betty", "Paul", "David", "Heather"};
String[] lastName = {"Ostendoft", "Bush", "Slater", "Gardner", "Rivers", "Lee", "Small"};
String[] address = {"123 Fort St.", "456 Comp St.", "789 Blue Ave", "3452 Missouri Rd.", "43418 Old River Rd.", "457 Addy Ct.", "4233 Harvey St."};
String[] city = {"Baltimore", "Dallas", "Fort Worth", "Owings Mills", "Miami", "Colorado Springs", "Los Angeles"};
String[] state = {"Maryland", "Texas", "Texas", "Maryland", "Florida", "Colorado", "California"};
String[] zipCode = {"21234", "12345", "67890", "32345", "43418", "31241", "31489", "83974"};
StreamReader iFile = new StreamReader("Address.txt");
for(int i = 0; i < lastName.Length; i++)
iFile.WriteLine(lastName[i] + "," + lastName[i] + "\n" + address[i] + "\n" +
city[i] + "\n" + state[i] + "\n" + zipCode[i]);
iFile.Close();
}
public frmAddress(string fName, string lName)
{
firstName = fName;
lastName = lName;
}
public string FName
{
get
{
return firstName;
}
}
public string LName
{
get
{
return lastName;
}
}
public string Address
{
get
{
return address;
}
}
public string City
{
get
{
return city;
}
}
public string State
{
get
{
return state;
}
}
public string Zip
{
get
{
return zipCode;
}
}
}
}