///////////////////////////////////////////////////////////////////////////// // TextEdit.cs - Demonstrates how to read, edit, and write files // // // // Languagae: C#, Visual Studio, .Net 2003 // // Platfrom: Dell Dimension 8300, Windows XP Pro, SP2 // // Author: Jim Fawcett, CST 2-187, Syracuse Univ., Syracuse, NY 13224 // // jfawcett@twcny.rr.com, (315)-443-3948 // ///////////////////////////////////////////////////////////////////////////// using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; namespace TextEdit { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.ComponentModel.Container components = null; private string currentFile; private System.Windows.Forms.MenuItem menuItem4; bool dirty = false; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.SuspendLayout(); // // textBox1 // this.textBox1.AcceptsReturn = true; this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.textBox1.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.textBox1.Location = new System.Drawing.Point(40, 40); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Size = new System.Drawing.Size(480, 400); this.textBox1.TabIndex = 0; this.textBox1.Text = ""; this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2, this.menuItem3, this.menuItem4}); this.menuItem1.Text = "File"; // // menuItem2 // this.menuItem2.Index = 0; this.menuItem2.Text = "open"; this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click); // // menuItem3 // this.menuItem3.Index = 1; this.menuItem3.Text = "new"; this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click); // // menuItem4 // this.menuItem4.Index = 2; this.menuItem4.Text = "save"; this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(560, 514); this.Controls.Add(this.textBox1); this.Menu = this.mainMenu1; this.Name = "Form1"; this.Text = "Form1"; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { this.Text = "Prototype Text Editor"; } //----< file\open menu item >------------------------------------ private void menuItem2_Click(object sender, System.EventArgs e) { FileDialog fd = new OpenFileDialog(); fd.InitialDirectory = Directory.GetCurrentDirectory(); if(fd.ShowDialog()==DialogResult.OK) { currentFile = fd.FileName; this.Text = currentFile; StreamReader sr = new StreamReader(currentFile); textBox1.Text = sr.ReadToEnd(); dirty = false; sr.Close(); } fd.Dispose(); } //----< file\save menu item >------------------------------------ private void menuItem4_Click(object sender, System.EventArgs e) { FileDialog fd = new SaveFileDialog(); fd.FileName = currentFile; if(fd.ShowDialog()==DialogResult.OK) { StreamWriter sw = new StreamWriter(fd.FileName); sw.WriteLine(textBox1.Text); sw.Close(); dirty = false; } fd.Dispose(); textBox1.SelectionStart = 0; textBox1.SelectionLength = 0; } //----< file\new menu item >------------------------------------- private void menuItem3_Click(object sender, System.EventArgs e) { textBox1.Text = ""; currentFile = "new.txt"; this.Text = currentFile; dirty = false; } //----< set dirty flag >----------------------------------------- private void textBox1_TextChanged(object sender, System.EventArgs e) { dirty = true; } //----< save modified text on closing >-------------------------- private void Form1_Closing(object sender, CancelEventArgs e) { if(dirty == true) menuItem4_Click(sender,e); } } }