Загрузка данных
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace zadanie8
{
public partial class Form1 : Form
{
RichTextBox editor = new RichTextBox();
string fileName = "";
public Form1()
{
InitializeComponent();
Text = "Текстовый редактор";
Size = new Size(800, 550);
MenuStrip menu = new MenuStrip();
ToolStripMenuItem file = new ToolStripMenuItem("Файл");
ToolStripMenuItem edit = new ToolStripMenuItem("Правка");
ToolStripMenuItem service = new ToolStripMenuItem("Сервис");
ToolStripMenuItem font = new ToolStripMenuItem("Шрифт");
ToolStripMenuItem about = new ToolStripMenuItem("О программе");
menu.Items.Add(file);
menu.Items.Add(edit);
menu.Items.Add(service);
menu.Items.Add(font);
menu.Items.Add(about);
editor.Dock = DockStyle.Fill;
editor.Font = new Font("Arial", 12);
Controls.Add(editor);
Controls.Add(menu);
MainMenuStrip = menu;
// Файл
file.DropDownItems.Add("Создать", null, (s, e) =>
{
if (CheckSave())
{
editor.Clear();
fileName = "";
editor.Modified = false;
}
});
file.DropDownItems.Add("Открыть", null, (s, e) =>
{
if (!CheckSave())
return;
OpenFileDialog d = new OpenFileDialog();
d.Filter = "RTF (*.rtf)|*.rtf|TXT (*.txt)|*.txt";
if (d.ShowDialog() == DialogResult.OK)
{
try
{
if (Path.GetExtension(d.FileName).ToLower() == ".rtf")
editor.LoadFile(d.FileName, RichTextBoxStreamType.RichText);
else
editor.LoadFile(d.FileName, RichTextBoxStreamType.PlainText);
fileName = d.FileName;
editor.Modified = false;
}
catch
{
MessageBox.Show("Не удалось открыть файл!");
}
}
});
file.DropDownItems.Add("Сохранить", null, (s, e) =>
{
SaveDocument(false);
});
file.DropDownItems.Add("Сохранить как", null, (s, e) =>
{
SaveDocument(true);
});
file.DropDownItems.Add("Выход", null, (s, e) =>
{
Close();
});
// Правка
edit.DropDownItems.Add("Отменить", null, (s, e) => editor.Undo());
edit.DropDownItems.Add("Повторить", null, (s, e) => editor.Redo());
edit.DropDownItems.Add("Вырезать", null, (s, e) => editor.Cut());
edit.DropDownItems.Add("Копировать", null, (s, e) => editor.Copy());
edit.DropDownItems.Add("Вставить", null, (s, e) => editor.Paste());
edit.DropDownItems.Add("Выделить всё", null, (s, e) => editor.SelectAll());
// Сервис
service.DropDownItems.Add("По левому краю", null, (s, e) =>
{
editor.SelectionAlignment = HorizontalAlignment.Left;
});
service.DropDownItems.Add("По центру", null, (s, e) =>
{
editor.SelectionAlignment = HorizontalAlignment.Center;
});
service.DropDownItems.Add("По правому краю", null, (s, e) =>
{
editor.SelectionAlignment = HorizontalAlignment.Right;
});
service.DropDownItems.Add("Цвет фона", null, (s, e) =>
{
ColorDialog d = new ColorDialog();
if (d.ShowDialog() == DialogResult.OK)
editor.BackColor = d.Color;
});
// Шрифт
font.DropDownItems.Add("Изменить шрифт", null, (s, e) =>
{
FontDialog d = new FontDialog();
d.Font = editor.SelectionFont ?? editor.Font;
if (d.ShowDialog() == DialogResult.OK)
editor.SelectionFont = d.Font;
});
font.DropDownItems.Add("Цвет текста", null, (s, e) =>
{
ColorDialog d = new ColorDialog();
if (d.ShowDialog() == DialogResult.OK)
editor.SelectionColor = d.Color;
});
// О программе
about.Click += (s, e) =>
{
MessageBox.Show(
"Простой текстовый редактор\nЗадание 8\nC# Windows Forms",
"О программе",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
};
// Проверка при закрытии окна
FormClosing += (s, e) =>
{
if (!CheckSave())
{
e.Cancel = true;
return;
}
DialogResult result = MessageBox.Show(
"Вы уверены, что хотите выйти?",
"Выход",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.No)
e.Cancel = true;
};
}
// Сохранение документа
bool SaveDocument(bool saveAs)
{
string path = fileName;
if (path == "" || saveAs)
{
SaveFileDialog d = new SaveFileDialog();
d.Filter = "RTF (*.rtf)|*.rtf|TXT (*.txt)|*.txt";
if (d.ShowDialog() == DialogResult.OK)
path = d.FileName;
else
return false;
}
try
{
if (Path.GetExtension(path).ToLower() == ".rtf")
editor.SaveFile(path, RichTextBoxStreamType.RichText);
else
editor.SaveFile(path, RichTextBoxStreamType.PlainText);
fileName = path;
editor.Modified = false;
return true;
}
catch
{
MessageBox.Show("Не удалось сохранить файл!");
return false;
}
}
// Проверка несохранённых изменений
bool CheckSave()
{
if (!editor.Modified)
return true;
DialogResult result = MessageBox.Show(
"В документе есть несохранённые изменения.\nЖелаете сохранить их?",
"Предупреждение",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Warning);
if (result == DialogResult.Cancel)
return false;
if (result == DialogResult.Yes)
return SaveDocument(false);
return true;
}
}
}