using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace FileReadOpenFileDialog
{
public class DisplayForm : System.Windows.Forms.Form
{
#region ...Omitted designer generated code
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.Label Label1;
internal System.Windows.Forms.Button nextButton;
internal System.Windows.Forms.Label phoneLabel;
internal System.Windows.Forms.Label nameLabel;
internal System.Windows.Forms.OpenFileDialog OpenFileDialog1;
internal System.Windows.Forms.MenuItem fileOpenMenuItem;
internal System.Windows.Forms.MenuItem fileExitMenuItem;
internal System.Windows.Forms.MainMenu MainMenu1;
internal System.Windows.Forms.MenuItem fileMenu;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public DisplayForm()
{
//
// 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.Label2 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.nextButton = new System.Windows.Forms.Button();
this.phoneLabel = new System.Windows.Forms.Label();
this.nameLabel = new System.Windows.Forms.Label();
this.OpenFileDialog1 = new System.Windows.Forms.OpenFileDialog();
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.SuspendLayout();
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(34, 98);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(56, 24);
this.Label2.TabIndex = 7;
this.Label2.Text = "Phone:";
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(34, 58);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(48, 16);
this.Label1.TabIndex = 5;
this.Label1.Text = "Name:";
//
// nextButton
//
this.nextButton.Enabled = false;
this.nextButton.Location = new System.Drawing.Point(138, 186);
this.nextButton.Name = "nextButton";
this.nextButton.TabIndex = 9;
this.nextButton.Text = "&Next";
this.nextButton.Click += new System.EventHandler(this.nextButton_Click);
//
// phoneLabel
//
this.phoneLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.phoneLabel.Location = new System.Drawing.Point(106, 98);
this.phoneLabel.Name = "phoneLabel";
this.phoneLabel.Size = new System.Drawing.Size(152, 23);
this.phoneLabel.TabIndex = 8;
//
// nameLabel
//
this.nameLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.nameLabel.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.nameLabel.Location = new System.Drawing.Point(106, 58);
this.nameLabel.Name = "nameLabel";
this.nameLabel.Size = new System.Drawing.Size(152, 23);
this.nameLabel.TabIndex = 6;
//
// OpenFileDialog1
//
this.OpenFileDialog1.DefaultExt = "txt";
this.OpenFileDialog1.Filter = ".txt files|*.txt|All files|*.*";
this.OpenFileDialog1.InitialDirectory = "Application.StartupPath";
this.OpenFileDialog1.Title = "Open File to Read";
//
// 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);
//
// DisplayForm
//
this.AcceptButton = this.nextButton;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(288, 225);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.Label2,
this.Label1,
this.nextButton,
this.phoneLabel,
this.nameLabel});
this.Menu = this.MainMenu1;
this.Name = "DisplayForm";
this.Text = "Display Name Phone File";
this.ResumeLayout(false);
}
#endregion
#endregion
private StreamReader phoneStreamReader;
[STAThread]
static void Main()
{
Application.Run(new DisplayForm());
}
private void nextButton_Click(object sender, System.EventArgs e)
{
//Read and display the next record
if (phoneStreamReader.Peek() != -1)
{
nameLabel.Text = phoneStreamReader.ReadLine();
phoneLabel.Text = phoneStreamReader.ReadLine();
}
}
private void fileOpenMenuItem_Click(object sender, System.EventArgs e)
{
//Display the Open File dialog box and open the file
//DialogResult responseDialogResult;
if (phoneStreamReader != null) //Is file already open?
{
phoneStreamReader.Close();
}
OpenFileDialog1.InitialDirectory = Application.StartupPath;
//Display the File Open dialog box
if (OpenFileDialog1.ShowDialog() != DialogResult.Cancel) //User didn't click Cancel button
{
phoneStreamReader = new StreamReader(OpenFileDialog1.FileName); //Open the input file
nextButton.Enabled = true;
nextButton_Click(sender, e); //Display first record
}
}
private void fileExitMenuItem_Click(object sender, System.EventArgs e)
{
//End the project
if (phoneStreamReader != null) //Is file open?
{
phoneStreamReader.Close();
}
this.Close();
}
}
}