//used when create new document to add a number to the document name like //"myDoc0.txt", so when you create another one it will be "myDoc1.txt" private int m_intDocNumber = 0; //Hold the file name that created or choosed //for the Save action, not "Save As..." private string m_strFileName = ""; //Hold the DialogResult enum result //Ok or Cancel private DialogResult dlgResult; //Stream to write to the file private StreamWriter m_sw; //Check if the file is modified or not private bool m_bModified = false;
//The TextChanged Event for the txtBody Control private void txtBody_TextChanged(object sender, System.EventArgs e) { m_bModified = true; //Document has been modified. }
//This Method check if the Text has beed modified or not //if yes display a message asking if the user wants to save his work or not private void CheckChanged(object sender, System.EventArgs e) { if(m_bModified) { dlgResult = MessageBox.Show("Do you want to save?","Note",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation); if(dlgResult == DialogResult.Yes) { //Calling the menuItemSave_Click Mthod // to save the current work document menuItemSave_Click(sender,e); } } }
private void menuItemNew_Click(object sender, System.EventArgs e) { // Check if the current work document is not // completely saved. CheckChanged(sender,e); // cleare the file name so when click on Save menu Item // it will not overwrite an old document m_strFileName = ""; // cleare the editor area. txtBody.Clear(); // new document means not modified yet. m_bModified = false; /* do not replace the m_bModified position in this method beacuse Clear() Method invoke the TextChanged event */ }
//Save As... private void menuItemSaveAs_Click(object sender, System.EventArgs e) { //dlgSaveFile is a SaveFileDialog object created from the toolbox //like mydoc0.txt dlgSaveFile.FileName = "mydoc" + m_intDocNumber.ToString() + ".txt"; dlgResult = dlgSaveFile.ShowDialog(); if(dlgResult == DialogResult.Cancel) return; try { // Saving our file name for Quick Save m_strFileName = dlgSaveFile.FileName; //Create new StreamWriter to write the text to it m_sw = new StreamWriter(m_strFileName); m_sw.Write (txtBody.Text); // don't forget to close your resources after finishing // using them m_sw.Close(); menuItemSave.Enabled = true; //increasing document number; m_intDocNumber++; m_bModified = false; //document is saved this.Text = "C# Simple Notepad: " + m_strFileName; } catch(Exception err) { MessageBox.Show(err.Message,"Error", MessageBoxButtons.OK,MessageBoxIcon.Error); } }
private void menuItemSave_Click(object sender, System.EventArgs e) { //Check if for the file name to save on the current working document or create new one if(m_strFileName == "") { //Create new one menuItemSaveAs_Click(sender,e); return; } else { //Save on the current document m_sw = new StreamWriter(m_strFileName); m_sw.Write (txtBody.Text); m_sw.Close(); } m_bModified = false; }
private void menuItemOpen_Click(object sender, System.EventArgs e) { //Check if the current document is modified to save it CheckChanged(sender,e); // dlgOpenFile is an OpenFileDialog object dlgResult = dlgOpenFile.ShowDialog(); if(dlgResult == DialogResult.Cancel) return; try { // get the file name m_strFileName = dlgOpenFile.FileName; // open to read that file StreamReader sr = new StreamReader(m_strFileName); //read the whole file txtBody.Text = sr.ReadToEnd(); sr.Close(); m_bModified = false; } catch(Exception err) { MessageBox.Show(err.Message,"Error", MessageBoxButtons.OK,MessageBoxIcon.Error); } } Now we want to change the font and its size & style: private void menuItemFont_Click(object sender, System.EventArgs e) { //dlgFont is an FontDialog object dlgResult = dlgFont.ShowDialog(); if(dlgResult == DialogResult.Cancel) return; // this property will return FontFamily selected from // the FontDialog FontFamily fmFontName = dlgFont.Font.FontFamily; // this property will return float // Font-size selected from the FontDialog float fFontSize = dlgFont.Font.Size; // this property will return // FontStyle selected from the FontDialog FontStyle fsStyle = dlgFont.Font.Style; Font font; try { //create new font and apply it to the text in txtBody font = new Font(fmFontName,fFontSize, txtBody.Font.Style ^ fsStyle); txtBody.Font = font; } catch(ArgumentException) { return; } }
Build Your Own ASP.NET Website Using C# & VB.NET