Advertisement
aslen

Untitled

May 10th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace NoteBook1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         private bool isFilePass;
  17.         private bool isFileSave;
  18.         private bool isDirty;
  19.         private string filePath;
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void MenuNewFileClick(object sender, EventArgs e)
  27.         {
  28.             if ((isFilePass && isFileSave) || (richTextBox1.Text == string.Empty))
  29.             {
  30.                 richTextBox1.Text = string.Empty;
  31.             }
  32.             else
  33.                 NewFileMessage();
  34.         }
  35.         private void NewFileMessage()
  36.         {
  37.             SaveFileDialog saveAs = new SaveFileDialog();
  38.             DialogResult dialogResult = MessageBox.Show("Do you want to save changes to file?", "My noteBook", MessageBoxButtons.YesNoCancel);
  39.             if (DialogResult.Yes == dialogResult)
  40.             {
  41.                 isFilePass = true;
  42.                 isFileSave = true;
  43.                 saveAs.ShowDialog();
  44.                 filePath = saveAs.FileName;
  45.                 SaverToFile();
  46.                 richTextBox1.Text = string.Empty;
  47.             }
  48.             else if (DialogResult.No == dialogResult)
  49.             {
  50.                 richTextBox1.Text = string.Empty;
  51.             }
  52.             else { }
  53.         }
  54.         private void SaverToFile()
  55.         {
  56.             if (isFilePass)
  57.             {
  58.                 using (StreamWriter saveToFile = new StreamWriter(filePath))
  59.                 {
  60.                     saveToFile.WriteLine(richTextBox1.Text);
  61.                 }
  62.             }
  63.             else
  64.                 NewFileMessage();
  65.         }
  66.  
  67.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  68.         {
  69.  
  70.         }
  71.  
  72.         private void MenuOpenFileClick(object sender, EventArgs e)
  73.         {
  74.             OpenFileDialog openFileDialog = new OpenFileDialog();
  75.             DialogResult dialogResult = openFileDialog.ShowDialog();
  76.             if (DialogResult.OK == dialogResult)
  77.             {
  78.                 isDirty = false;
  79.                 using (StreamReader readFromFile = new StreamReader(openFileDialog.FileName))
  80.                 {
  81.                     richTextBox1.Text = readFromFile.ReadToEnd();
  82.                 }
  83.                 filePath = openFileDialog.FileName;
  84.             }
  85.         }
  86.  
  87.         private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  88.         {
  89.             if (!isDirty)
  90.                 return;
  91.             if (string.IsNullOrEmpty(filePath))
  92.             {
  93.                 MenuSaveFileClick();
  94.             }
  95.         }
  96.         private void MenuSaveFileClick()
  97.         {
  98.             if (isDirty)
  99.             {
  100.                 SaveFileDialog saveAs = new SaveFileDialog();
  101.                 DialogResult dialogResult = saveAs.ShowDialog();
  102.                 if (DialogResult.OK == dialogResult)
  103.                 {
  104.                     isFilePass = true;
  105.                     isFileSave = true;
  106.                     filePath = saveAs.FileName;
  107.                     SaverToFile();
  108.                 }
  109.             }
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement