8 Replies - 2622 Views - Last Post: 13 August 2009 - 06:01 PM Rate Topic: -----

#1 phillover80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-June 09

Trying to create address book using txt file

Post icon  Posted 09 August 2009 - 07:22 PM

I have been trying this for a few days now and I am not even sure if I am going about this right. 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. In the below code, I've chose to do 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'm trying to figure out the best way to try and go about this as I am extremely confused about this one. How do I get it to save when someone clicks save on the menu?
Can anyone help?
I don't have much yet. 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 Final_Project
{
	/// <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.ListBox lstBoxDisplay;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		

		public frmAddress()
		{
			
		}

		private void menuSave_Click(object sender, System.EventArgs e)
		{
			

		}
		
		
		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.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.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.menuOpen,
																					 this.menuSave});
			this.menuFile.Text = "File";
			// 
			// menuOpen
			// 
			this.menuOpen.Index = 0;
			this.menuOpen.Text = "Open";
			this.menuOpen.Click += new System.EventHandler(this.menuOpen_Click);
			// 
			// menuSave
			// 
			this.menuSave.Index = 1;
			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";
			// 
			// 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";
			// 
			// 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";
			// 
			// 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";
			// 
			// lstBoxDisplay
			// 
			this.lstBoxDisplay.Location = new System.Drawing.Point(30, 30);
			this.lstBoxDisplay.Name = "lstBoxDisplay";
			this.lstBoxDisplay.Size = new System.Drawing.Size(230, 95);
			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)
		{
							
			StreamReader iFile = new StreamReader("Address.txt");

			private string firstName;
			private string lastName;
			private string address;
			private string city;
			private string state;
			private string zipCode;
			
			public frmAddress()
			{
			}	

			public frmAddress(string lName, string fName)
			{
				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;
				}
			}

		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to create address book using txt file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Trying to create address book using txt file

Posted 09 August 2009 - 08:47 PM

Whenever a UI element is clicked on, an event is generated. You need to handle any event in which you're interested, like the Save menu item being clicked, in whatever way you deem necessary.
Was This Post Helpful? 0
  • +
  • -

#3 phillover80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-June 09

Re: Trying to create address book using txt file

Posted 11 August 2009 - 06:14 PM

Ok this is what I came up with. Not getting errors but nothing is showing up when I hit the display all or display contact button. My Open even gives me the openDialog box but does not open the file.
Can anyone help? I'm getting there but this is so fustrating.

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 menuFile;
		private System.Windows.Forms.MenuItem menuOpen;
		private System.Windows.Forms.MenuItem menuSave;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.ListBox lstBoxDisplay;
		private System.Windows.Forms.Button btnAdd;
		private System.Windows.Forms.Button btnDisplayAll;
		private System.Windows.Forms.Button btnDelete;
		private StreamReader iFile;
		private StreamWriter oFile;

		private string lastName;
		private string firstName;
		private string address;
		private string city;
		private string state;
		private string zipCode;
		private System.Windows.Forms.Button btnDisplayContact;
		private System.Windows.Forms.SaveFileDialog saveFD;
		private System.Windows.Forms.OpenFileDialog openFD;



		private System.ComponentModel.Container components = null;

		public frmAddress()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
		
		}

		private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
		
		}

		private void btnDisplayRecord_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 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.Length < 1)
			{
				MessageBox.Show("There are no items to delete");
			}
			else if (lstBoxDisplay.Text == "")
			{
				MessageBox.Show("Please select an item to delete");
			}
			else
			
				lstBoxDisplay.Items.RemoveAt(lstBoxDisplay.SelectedIndex);  
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			this.lstBoxDisplay.Items.Add(lastName);
			this.lstBoxDisplay.Items.Add(firstName);
			this.lstBoxDisplay.Items.Add(address);
			this.lstBoxDisplay.Items.Add(city);
			this.lstBoxDisplay.Items.Add(state);
			this.lstBoxDisplay.Items.Add(zipCode);
		}

		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.FileName;
				
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuFile = new System.Windows.Forms.MenuItem();
			this.menuOpen = new System.Windows.Forms.MenuItem();
			this.menuSave = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.lstBoxDisplay = new System.Windows.Forms.ListBox();
			this.btnAdd = new System.Windows.Forms.Button();
			this.btnDisplayAll = new System.Windows.Forms.Button();
			this.btnDelete = new System.Windows.Forms.Button();
			this.btnDisplayContact = new System.Windows.Forms.Button();
			this.saveFD = new System.Windows.Forms.SaveFileDialog();
			this.openFD = new System.Windows.Forms.OpenFileDialog();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuFile});
			// 
			// menuFile
			// 
			this.menuFile.Index = 0;
			this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuOpen,
																					 this.menuItem4,
																					 this.menuSave});
			this.menuFile.Text = "File";
			// 
			// menuOpen
			// 
			this.menuOpen.Index = 0;
			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);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 1;
			this.menuItem4.Text = "-";
			// 
			// lstBoxDisplay
			// 
			this.lstBoxDisplay.Location = new System.Drawing.Point(15, 20);
			this.lstBoxDisplay.Name = "lstBoxDisplay";
			this.lstBoxDisplay.Size = new System.Drawing.Size(255, 108);
			this.lstBoxDisplay.TabIndex = 0;
			// 
			// btnAdd
			// 
			this.btnAdd.Location = new System.Drawing.Point(20, 160);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.Size = new System.Drawing.Size(100, 23);
			this.btnAdd.TabIndex = 1;
			this.btnAdd.Text = "Add Contact";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// btnDisplayAll
			// 
			this.btnDisplayAll.Location = new System.Drawing.Point(20, 220);
			this.btnDisplayAll.Name = "btnDisplayAll";
			this.btnDisplayAll.Size = new System.Drawing.Size(100, 23);
			this.btnDisplayAll.TabIndex = 2;
			this.btnDisplayAll.Text = "Display All";
			this.btnDisplayAll.Click += new System.EventHandler(this.btnDisplayAll_Click);
			// 
			// btnDelete
			// 
			this.btnDelete.Location = new System.Drawing.Point(160, 160);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(100, 23);
			this.btnDelete.TabIndex = 3;
			this.btnDelete.Text = "Delete Contact";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnDisplayContact
			// 
			this.btnDisplayContact.Location = new System.Drawing.Point(160, 220);
			this.btnDisplayContact.Name = "btnDisplayContact";
			this.btnDisplayContact.Size = new System.Drawing.Size(100, 23);
			this.btnDisplayContact.TabIndex = 4;
			this.btnDisplayContact.Text = "Display Contact";
			this.btnDisplayContact.Click += new System.EventHandler(this.btnDisplayRecord_Click);
			// 
			// saveFD
			// 
			this.saveFD.FileOk += new System.ComponentModel.CancelEventHandler(this.saveFileDialog1_FileOk);
			// 
			// openFD
			// 
			this.openFD.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
			// 
			// 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, 284);
			this.Controls.Add(this.btnDisplayContact);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnDisplayAll);
			this.Controls.Add(this.btnAdd);
			this.Controls.Add(this.lstBoxDisplay);
			this.Menu = this.mainMenu1;
			this.Name = "frmAddress";
			this.Text = "Address Book";
			this.Load += new System.EventHandler(this.frmAddress_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmAddress());
		}

		private void frmAddress_Load(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");
			StreamWriter oFile = new StreamWriter("Address2.txt");
			for(int i = 0; i < lastName.Length; i++)				
				
				oFile.Write(String.Format((lastName[i] + "," + lastName[i] + "\n" + address[i] + "\n" +
					city[i] + "\n" + state[i] + "\n" + zipCode[i])));
			oFile.Close();
			oFile.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;
			}	
		}
	

		private void menuOpen_Click(object sender, System.EventArgs e)
		{
			string Chosen_File = "Address.txt";
			openFD.InitialDirectory = "";
			openFD.Title = "Open";
			openFD.FileName = "Address.txt";
			openFD.Filter = "Text Files|*.txt";
			if (openFD.ShowDialog() != DialogResult.Cancel)
				{
				Chosen_File = openFD.FileName;
				}
				
		}
	}
}



This post has been edited by phillover80: 12 August 2009 - 04:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 sh3ll3y  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-July 09

Re: Trying to create address book using txt file

Posted 12 August 2009 - 03:30 AM

View Postphillover80, on 11 Aug, 2009 - 05:14 PM, said:

Ok this is what I came up with. Of course I am getting a ton of errors because for one thing, I am not exactly how to get the data from the text file and store it into classes.

Can anyone help? This is so fustrating.

	
	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;
			}	
		}
		
	}
}	




Hey phill i haven't read your code coz i gotta leave urgently now
but what i can suggest is you get all the information from the user and store it in a text file
let a single line contain all the information for one user and separate each info with a '|' character
as u said u use StreamWriter to write into a text file and StreamReader to read.
writing should not be a problem for you . and when u read use the Split function to identify each field
u should use a class which holds these info in objects
to read
use a LIST<> to hold the objects of a person class
List<People> people=new <People>();
//to read
whille(textIn.Peek() !=-1)
{
string row=textIn.ReadLine();
string columns[]=row.Split('|');
Person person=new Person();
person.Name=column[0];
person.Age=Convert.ToInt(column[1]);
person......
//write all the properties
people.Add(person);
}
textIn.Close;
return people;


this will add all the person object and use can get them easily in the calling class using a foreach loop
goodluck
Was This Post Helpful? 0
  • +
  • -

#5 phillover80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-June 09

Re: Trying to create address book using txt file

Posted 12 August 2009 - 04:47 AM

Hi Shelley:
I've gotten it to compile since the last time I posted. I edited my last post with the new code so I don't make this thread any longer.

I cannot get anything to display though. See above post I edited.

This is the text file I am using. (not real people)

Ostendorf, Julie
123 Fort St.
Baltimore
Maryland
21234
Bush, Tony
456 Comp St.
Dallas
Texas
12345
Slater, Frederick
789 Blue Ave.
Fort Worth
Texas
67890
Gardner, Betty
3452 Missouri Rd.
Owings Mills
Maryland
32345
Rivers, Paul
43418 Old River Rd.
Miami
Florida
31241
Lee, David
457 Addy Ct.
Colorado Springs
Colorado
31489
Small, Heather
4233 Harvey St.
Los Angeles
California
83974

Should I put a special character after each address block of name, address, city, state and zip?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Trying to create address book using txt file

Posted 12 August 2009 - 05:52 AM

Here's your load method:
 private void frmAddress_Load(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");
            StreamWriter oFile = new StreamWriter("Address2.txt");
            for(int i = 0; i < lastName.Length; i++)                
               
                oFile.Write(String.Format((lastName[i] + "," + lastName[i] + "\n" + address[i] + "\n" +
                    city[i] + "\n" + state[i] + "\n" + zipCode[i])));
            oFile.Close();
            oFile.Close();
        }



Take a few minutes and look at each line separately and tell us what it is doing, and compare that to what you want it to do.
Was This Post Helpful? 0
  • +
  • -

#7 phillover80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-June 09

Re: Trying to create address book using txt file

Posted 12 August 2009 - 05:09 PM

I'm reposting the code because I noticed I was writing to the last name field twice and i wrote oFile.Close twice when I meant one of those to be oFile.Flush();

What I want it to do is read the data from the text file (Address.txt) and store the data into an array. My btnDisplayAll which is supposed to display all the information about a contact (first and last name, address, city, etc.) and my btnDisplayContact which is supposed to display the whole address book (names only in alpha order) are not working either. I get a blank messageBox so I was thinking it was because it is not storing the data from the text file to the array? I'm trying to pull everything together but get even more confused in the process.

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");
			StreamWriter oFile = new StreamWriter("Address2.txt");
			for(int i = 0; i < lastName.Length; i++)				
				
				oFile.Write(String.Format((lastName[i] + "," + firstName[i] + "\n" + address[i] + "\n" +
					city[i] + "\n" + state[i] + "\n" + zipCode[i])));
			oFile.Flush();
			oFile.Close();
			iFile.Close();


This post has been edited by phillover80: 12 August 2009 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Trying to create address book using txt file

Posted 12 August 2009 - 05:24 PM

Is this still your *load* function? If so...you're not paying attention.
Was This Post Helpful? 0
  • +
  • -

#9 rapunzel2779  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 09

Re: Trying to create address book using txt file

Posted 13 August 2009 - 06:01 PM

I have this same exact project to do. What shool do you go to?

I was hoping to read my data into arrays but i cannot figure out how to input the right information into the proper array.

Anyone have any ideas?

if my .txt file said:
Miller, Mike
1234 Main St
Cleveland
Ohio
44109

How would i get the StreamReader to read each line and insert it into the proper array?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1