using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text; using System.Collections.Specialized; using System.IO;
A summary description of Namespace WindowsApplication14 {///
// .. private system.componentmodel.container component = null; private system.windows.Forms.savefiledialog savefiledialog; private string m_filename = ""
///
Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();
// // Todo: Add any constructor code after INITIALIZEComponent call //}
///
#REGION Windows Form Designer The code ///
/// summary> private void InitializeComponent () {this.mainMenu1 = new System.Windows.Forms.MainMenu (); this.menuFile = new System.Windows.Forms.MenuItem (); this.menuFileOpen = new System. Windows.Forms.MenuItem (); this.txt = new system.windows.forms.textbox (); this.Menuitem1 = new system.windows.forms.MenuItem (); this.MenufileExit = new System.Windows.Forms.MenuItem.Windows.Forms.Menuitem (); this.openFileDialog = new System.Windows.Forms.OpenFileDialog (); this.menuFileSave = new System.Windows.Forms.MenuItem (); this.menuFileSaveAs = new System.Windows.Forms.MenuItem (); this. saveFileDialog = new System.Windows.Forms.SaveFileDialog (); 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.MenuFileOpen, this.me nuFileSave, this.menuFileSaveAs, this.menuItem1, this.menuFileExit}); this.menuFile.Text = "& File"; // // menuFileOpen // this.menuFileOpen.Index = 0; this.menuFileOpen.Shortcut = System.Windows .Form.Shortcut.ctrlo; this.MenufileOpen.text = "& open"; this.MenufileOpen.Click = new system.eventhandler (this.MenuFileOpen_Click; // // txt // this.txt.dock = system.windows .Forms.dockstyle.fill; this.txt.location = new system.drawing.point (0, 0); this.txt.Multiline = true; this.txt.name = "txt"; this.txt.scrollbars =
System.windows.Forms.Scrollbars.both; this.txt.size = new system.drawing.size (292, 273); this.txt.TabINDEX = 0; this.txt.text = ""; //// Menuitem1 // this.Menuitem1.index = 3; this.Menuitem1.text = "-"; /////ufileexit // this.MenufileExit.index = 4; this.MenufileExit.shortcut = system.windows.Forms.shortcut.Artf4 ; this.menuFileExit.Text = "E & xit"; this.menuFileExit.Click = new System.EventHandler (this.menuFileExit_Click); // // menuFileSave // this.menuFileSave.Index = 1; this.menuFileSave.Shortcut = System .Windows.Forms.Shortcut.CtrlS; this.menuFileSave.Text = "& Save"; this.menuFileSave.Click = new System.EventHandler (this.menuFileSave_Click); // // menuFileSaveAs // this.menuFileSaveAs.Index = 2 ; this.menuFileSaveAs.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftS; this.menuFileSaveAs.Text = "Save & As"; this.menuFileSaveAs.Click = new System.EventHandler (this.menuFileSaveAs_Click); // // Form1 // t His.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = new system.drawing.size (292, 273); this.Controls.add (this.txt); this.Menu = this.mainu1 This.name = "form1"; this.text = "form1"; this.closing = new system.componentmodel.canceleventhandler (this.form1_closing); this.ResumeLayout (false);} #ENDREGION
///
Void setModified (Bool Bmodified) {txt.modified = bmodified;
bool IsModified () {return txt.Modified;} private void SaveFile (string filePath) {try {StreamWriter aStreamWriter = new StreamWriter (filePath, false); int I; for (I = 0; txt.Lines.Length> I; I ) {ASTREAMWRITER.WRITELINE (txt.lines [i]);} astreamwriter.close ();} catch (exception ex) {messagebox.show (ex.Message, "Error Saving File.");}} Private void savefilewithname ! string filePath) {if (filePath = null) {SaveFile (filePath); m_FileName = filePath; SetModified (false);} else {SaveFileDialog saveFileDialog = new SaveFileDialog (); saveFileDialog.Filter = "Text File (* txt) |. * .txt | All File (* *.) | * * ";. saveFileDialog.FilterIndex = 1; saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog () == DialogResult.OK) {SaveFile (saveFileDialog.FileName); M_FileName = SaveFileDialog.FileName; setmodified (true);}}}
Private void readfile (string filepath) {const Int maxtextsize = 65535;
FileStream FileStream = New FileStream (FilePath, FileMode.open, FileAccess.Readwrite; StreamReader StreamReader = New StreamReader;
StringCollection stringCollection = new StringCollection (); int noByteRead = 0; string oneLine; while (! (OneLine = streamReader.ReadLine ()) = null) {noByteRead = oneLine.Length; if (noByteRead> MaxTextSize) break; stringCollection.Add ( "} streamreader.close ();
String [] stringArray = new string [stringcollection.count]; stringcollection.copyto (stringArray, 0); txt.line = stringArray;}
private void menuFileOpen_Click (object sender, System.EventArgs e) {OpenFileDialog openFileDialog = new OpenFileDialog (); openFileDialog.Filter = ". Text File (. * txt) | * .txt | All File (. * *) | * *" ; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog () == DialogResult.OK) {try {ReadFile (openFileDialog.FileName); m_FileName = openFileDialog.FileName; SetModified (false);} catch ( Exception ex) {messagebox.show (ex.Message, "Error Reading File");}}} private void menufileexit_click (object sender, system.eventargs e) {base.dispose ();
Private void menufilesave_click (object sender, system.eventargs e) {savefilewithname;}
Private void menufilesaveas_click (object repender, system.eventargs e) {savefilewithname (null);}
private void Form1_Closing (object sender, System.ComponentModel.CancelEventArgs e) {if (IsModified ()) {DialogResult aDialogResult = new DialogResult (); aDialogResult = MessageBox.Show ( "Would you like to save the file" m_FileName "?" , "SAVE FILE?", MessageBoxButtons.yesnocancel;
Canceleventargs AcanceleventArgs = (Canceleventargs) E;
switch (aDialogResult) {case DialogResult.Yes: SaveFileWithName (m_FileName); aCancelEventArgs.Cancel = false; break; case DialogResult.No: aCancelEventArgs.Cancel = false; break; case DialogResult.Cancel: aCancelEventArgs.Cancel = true; break;} }}}}