Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- namespace NoteBook1
- {
- public partial class Form1 : Form
- {
- private bool isFilePass;
- private bool isFileSave;
- private bool isDirty;
- private string filePath;
- public Form1()
- {
- InitializeComponent();
- }
- private void MenuNewFileClick(object sender, EventArgs e)
- {
- if ((isFilePass && isFileSave) || (richTextBox1.Text == string.Empty))
- {
- richTextBox1.Text = string.Empty;
- }
- else
- NewFileMessage();
- }
- private void NewFileMessage()
- {
- SaveFileDialog saveAs = new SaveFileDialog();
- DialogResult dialogResult = MessageBox.Show("Do you want to save changes to file?", "My noteBook", MessageBoxButtons.YesNoCancel);
- if (DialogResult.Yes == dialogResult)
- {
- isFilePass = true;
- isFileSave = true;
- saveAs.ShowDialog();
- filePath = saveAs.FileName;
- SaverToFile();
- richTextBox1.Text = string.Empty;
- }
- else if (DialogResult.No == dialogResult)
- {
- richTextBox1.Text = string.Empty;
- }
- else { }
- }
- private void SaverToFile()
- {
- if (isFilePass)
- {
- using (StreamWriter saveToFile = new StreamWriter(filePath))
- {
- saveToFile.WriteLine(richTextBox1.Text);
- }
- }
- else
- NewFileMessage();
- }
- private void richTextBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void MenuOpenFileClick(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- DialogResult dialogResult = openFileDialog.ShowDialog();
- if (DialogResult.OK == dialogResult)
- {
- isDirty = false;
- using (StreamReader readFromFile = new StreamReader(openFileDialog.FileName))
- {
- richTextBox1.Text = readFromFile.ReadToEnd();
- }
- filePath = openFileDialog.FileName;
- }
- }
- private void saveToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (!isDirty)
- return;
- if (string.IsNullOrEmpty(filePath))
- {
- MenuSaveFileClick();
- }
- }
- private void MenuSaveFileClick()
- {
- if (isDirty)
- {
- SaveFileDialog saveAs = new SaveFileDialog();
- DialogResult dialogResult = saveAs.ShowDialog();
- if (DialogResult.OK == dialogResult)
- {
- isFilePass = true;
- isFileSave = true;
- filePath = saveAs.FileName;
- SaverToFile();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement