Загрузка данных
8
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) =>
{
editor.Clear();
fileName = "";
});
file.DropDownItems.Add("Открыть", null, (s, e) =>
{
OpenFileDialog d = new OpenFileDialog();
d.Filter = "RTF (*.rtf)|*.rtf|TXT (*.txt)|*.txt";
if (d.ShowDialog() == DialogResult.OK)
{
fileName = d.FileName;
if (Path.GetExtension(fileName).ToLower() == ".rtf")
editor.LoadFile(fileName, RichTextBoxStreamType.RichText);
else
editor.LoadFile(fileName, RichTextBoxStreamType.PlainText);
}
});
file.DropDownItems.Add("Сохранить", null, (s, e) =>
{
SaveDocument();
});
file.DropDownItems.Add("Сохранить как", null, (s, e) =>
{
fileName = "";
SaveDocument();
});
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)
{
BackColor = d.Color;
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);
};
}
void SaveDocument()
{
if (fileName == "")
{
SaveFileDialog d = new SaveFileDialog();
d.Filter = "RTF (*.rtf)|*.rtf|TXT (*.txt)|*.txt";
if (d.ShowDialog() == DialogResult.OK)
fileName = d.FileName;
else
return;
}
if (Path.GetExtension(fileName).ToLower() == ".rtf")
editor.SaveFile(fileName, RichTextBoxStreamType.RichText);
else
editor.SaveFile(fileName, RichTextBoxStreamType.PlainText);
}
}
}
9
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace zadanie9
{
public partial class Form1 : Form
{
static Form1[] windows = new Form1[5];
static Random random = new Random();
RichTextBox editor = new RichTextBox();
Button button = new Button();
ToolTip tip = new ToolTip();
string fileName = "";
int number;
int next;
public Form1()
{
CreateWindow(0);
windows[0] = this;
for (int i = 1; i < 5; i++)
windows[i] = new Form1(i);
ChooseNext();
}
public Form1(int n)
{
CreateWindow(n);
}
void CreateWindow(int n)
{
InitializeComponent();
number = n;
Text = "Окно " + (number + 1);
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);
Panel panel = new Panel();
panel.Dock = DockStyle.Bottom;
panel.Height = 50;
button.Text = "Перейти в другое окно";
button.Size = new Size(200, 30);
button.Location = new Point(10, 10);
panel.Controls.Add(button);
Controls.Add(editor);
Controls.Add(panel);
Controls.Add(menu);
MainMenuStrip = menu;
file.DropDownItems.Add("Создать", null, (s, e) =>
{
editor.Clear();
fileName = "";
});
file.DropDownItems.Add("Открыть", null, (s, e) =>
{
OpenFileDialog d = new OpenFileDialog();
d.Filter = "RTF (*.rtf)|*.rtf|TXT (*.txt)|*.txt";
if (d.ShowDialog() == DialogResult.OK)
{
fileName = d.FileName;
if (Path.GetExtension(fileName).ToLower() == ".rtf")
editor.LoadFile(fileName, RichTextBoxStreamType.RichText);
else
editor.LoadFile(fileName, RichTextBoxStreamType.PlainText);
}
});
file.DropDownItems.Add("Сохранить", null, (s, e) =>
{
SaveDocument();
});
file.DropDownItems.Add("Сохранить как", null, (s, e) =>
{
fileName = "";
SaveDocument();
});
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)
{
BackColor = d.Color;
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Окно " + (number + 1) +
"\nЗадания 8 и 9",
"О программе",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
};
button.Click += (s, e) =>
{
Hide();
windows[next].ChooseNext();
windows[next].Show();
windows[next].Activate();
};
FormClosed += (s, e) =>
{
Application.Exit();
};
}
void ChooseNext()
{
do
{
next = random.Next(0, 5);
}
while (next == number);
tip.SetToolTip(button, "Перейти к окну " + (next + 1));
}
void SaveDocument()
{
if (fileName == "")
{
SaveFileDialog d = new SaveFileDialog();
d.Filter = "RTF (*.rtf)|*.rtf|TXT (*.txt)|*.txt";
if (d.ShowDialog() == DialogResult.OK)
fileName = d.FileName;
else
return;
}
if (Path.GetExtension(fileName).ToLower() == ".rtf")
editor.SaveFile(fileName, RichTextBoxStreamType.RichText);
else
editor.SaveFile(fileName, RichTextBoxStreamType.PlainText);
}
}
}