using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace FileWriteOpenFileDialog
{
public class PhoneForm : System.Windows.Forms.Form
{
#region ...Omitted designer generated code
internal System.Windows.Forms.Button saveButton;
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.Label Label1;
internal System.Windows.Forms.TextBox phoneTextBox;
internal System.Windows.Forms.TextBox nameTextBox;
internal System.Windows.Forms.MenuItem fileOpenMenuItem;
internal System.Windows.Forms.MenuItem fileExitMenuItem;
internal System.Windows.Forms.OpenFileDialog OpenFileDialog1;
internal System.Windows.Forms.MenuItem fileMenu;
internal System.Windows.Forms.MainMenu MainMenu1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

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

/// <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.saveButton = new System.Windows.Forms.Button();
this.Label2 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.phoneTextBox = new System.Windows.Forms.TextBox();
this.nameTextBox = new System.Windows.Forms.TextBox();
this.MainMenu1 = new System.Windows.Forms.MainMenu();
this.fileMenu = new System.Windows.Forms.MenuItem();
this.fileOpenMenuItem = new System.Windows.Forms.MenuItem();
this.fileExitMenuItem = new System.Windows.Forms.MenuItem();
this.OpenFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// saveButton
//
this.saveButton.Location = new System.Drawing.Point(185, 174);
this.saveButton.Name = "saveButton";
this.saveButton.TabIndex = 4;
this.saveButton.Text = "&Save";
this.saveButton.Click += new System.EventHandler(this.saveButton_Click);
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(33, 110);
this.Label2.Name = "Label2";
this.Label2.TabIndex = 2;
this.Label2.Text = "&Phone";
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(33, 70);
this.Label1.Name = "Label1";
this.Label1.TabIndex = 0;
this.Label1.Text = "&Name";
//
// phoneTextBox
//
this.phoneTextBox.Location = new System.Drawing.Point(153, 110);
this.phoneTextBox.Name = "phoneTextBox";
this.phoneTextBox.TabIndex = 3;
this.phoneTextBox.Text = "";
//
// nameTextBox
//
this.nameTextBox.Location = new System.Drawing.Point(153, 70);
this.nameTextBox.Name = "nameTextBox";
this.nameTextBox.TabIndex = 1;
this.nameTextBox.Text = "";
//
// MainMenu1
//
this.MainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileMenu});
//
// fileMenu
//
this.fileMenu.Index = 0;
this.fileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileOpenMenuItem,
this.fileExitMenuItem});
this.fileMenu.Text = "&File";
//
// fileOpenMenuItem
//
this.fileOpenMenuItem.Index = 0;
this.fileOpenMenuItem.Text = "&Open";
this.fileOpenMenuItem.Click += new System.EventHandler(this.fileOpenMenuItem_Click);
//
// fileExitMenuItem
//
this.fileExitMenuItem.Index = 1;
this.fileExitMenuItem.Text = "E&xit";
this.fileExitMenuItem.Click += new System.EventHandler(this.fileExitMenuItem_Click);
//
// OpenFileDialog1
//
this.OpenFileDialog1.CheckFileExists = false;
this.OpenFileDialog1.CheckPathExists = false;
this.OpenFileDialog1.DefaultExt = "txt";
this.OpenFileDialog1.FileName = "PhoneList";
this.OpenFileDialog1.Filter = "Text Files (*.txt) files|*.txt|All files (*.*)|*.*";
this.OpenFileDialog1.Title = "Open File";
//
// PhoneForm
//
this.AcceptButton = this.saveButton;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(296, 233);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.saveButton,
this.Label2,
this.Label1,
this.phoneTextBox,
this.nameTextBox});
this.Menu = this.MainMenu1;
this.Name = "PhoneForm";
this.Text = "Create Name Phone File";
this.ResumeLayout(false);

}
#endregion
#endregion

StreamWriter phoneStreamWriter;

[STAThread]
static void Main()
{
Application.Run(new PhoneForm());
}

private void saveButton_Click(object sender, System.EventArgs e)
{
//Save the record to the file

if (phoneStreamWriter != null) //Is the file open?
{
//Save the record
phoneStreamWriter.WriteLine(nameTextBox.Text);
phoneStreamWriter.WriteLine(phoneTextBox.Text);
nameTextBox.Clear();
nameTextBox.Focus();
phoneTextBox.Clear();
}
else //File is not open
{
MessageBox.Show("You must open the file before you can save a record",
"File Not Open", MessageBoxButtons.OK, MessageBoxIcon.Information);
fileOpenMenuItem_Click(sender, e); //Display the File Open dialog box
}
}

private void fileOpenMenuItem_Click(object sender, System.EventArgs e)
{
//Open the file

if (phoneStreamWriter != null) //Is the file already open?
{
phoneStreamWriter.Close();
}

OpenFileDialog1.InitialDirectory = Application.StartupPath; //Begin in project folder
//Display the File Open dialog box
if (OpenFileDialog1.ShowDialog() != DialogResult.Cancel) //User didn't click Cancel button
{
phoneStreamWriter = new StreamWriter(OpenFileDialog1.FileName); //Open the output file
}
nameTextBox.Focus();
}

private void fileExitMenuItem_Click(object sender, System.EventArgs e)
{
//Close the file and the form

if (phoneStreamWriter != null) //Is the file open?
{
phoneStreamWriter.Close();
}
this.Close();
}
}
}