public static DialogResult OpenFile(TextDocument doc)
        {
            OpenFileDialog ofg = new OpenFileDialog()
            {
                CheckFileExists = true,
                CheckPathExists = true,
                ValidateNames = true,
                Title = "Open File - MDI Sample",
                Filter = "Text files (*.txt)|*.txt"
            };

            if (ofg.ShowDialog() == DialogResult.OK)
            {
                doc.Location = ofg.FileName;

                try
                {
                    TextReader textReader = new StreamReader(doc.Location, doc.TextEncoding, true);
                    doc.Text = textReader.ReadToEnd();
                    textReader.Close();

                    return DialogResult.OK;
                }
                catch (Exception exception)
                {
                    MessageBox.Show("Ошибка открытия файла!/n" + exception.Message, "MDI Sample", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            return DialogResult.Cancel;
        }
Beispiel #2
0
        public TextEditor() 
        {
            InitializeComponent();

            document = new TextDocument();
            document.TextChanged += new EventHandler(document_TextChanged);
        }
        public static void ReloadFile(TextDocument doc, Encoding encoding)
        {
            doc.TextEncoding = encoding;

            if (doc.Location != String.Empty) ReloadFile(doc);
            else OpenFile(doc);
        }
 private static void ReloadFile(TextDocument doc)
 {
     try
     {
         TextReader textReader = new StreamReader(doc.Location, doc.TextEncoding, true);
         doc.Text = textReader.ReadToEnd();
         textReader.Close();
     }
     catch (Exception exception)
     {
         MessageBox.Show("Ошибка открытия файла!/n" + exception.Message, "MDI Sample", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
        private static void Save(TextDocument doc)
        {
            try
            {
                TextWriter textWriter = new StreamWriter(doc.Location, false, doc.TextEncoding);
                textWriter.Write(doc.Text);
                textWriter.Close();

                doc.isSaved = true;
            }
            catch
            {
                MessageBox.Show("Ошибка сохранения файла!", "MDI Sample", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public static void SaveFileAs(TextDocument doc)
        {
            SaveFileDialog sfd = new SaveFileDialog()
            {
                CheckPathExists = true,
                ValidateNames = true,
                AddExtension = true,
                Title = "Save File - MDI Sample",
                Filter = "Text files (*.txt)|*.txt"
            };

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                doc.Location = sfd.FileName;
                TextDocumentSaver.Save(doc);
            }
        }
 public static void SaveFileInEncoding(TextDocument doc, Encoding encoding)
 {
     doc.TextEncoding = encoding;
     SaveFile(doc);
 }
 public static void SaveFile(TextDocument doc)
 {
     if (doc.Location != String.Empty) Save(doc);
     else SaveFileAs(doc);
 }