-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormMain.cs
62 lines (55 loc) · 2.13 KB
/
FormMain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
namespace SyntaxHighlighterCS
{
public partial class FormMain : Form
{
private readonly HighlighterRules highlighterRules;
public FormMain()
{
InitializeComponent();
comboBox_syntaxOptions.DrawMode = DrawMode.OwnerDrawFixed;
comboBox_syntaxOptions.DrawItem += comboBox_syntaxOptions_DrawItem;
FillComboBoxWithEnum();
highlighterRules = new HighlighterRules(richTextBox_Editor);
richTextBox_Editor.TextChanged += (sender, e) => highlighterRules.HighlightSyntax();
}
private void comboBox_syntaxOptions_DrawItem(object? sender, DrawItemEventArgs? e)
{
e.DrawBackground();
if (e.Index >= 0)
{
string text = comboBox_syntaxOptions.Items[e.Index].ToString();
using Brush br = new SolidBrush(e.ForeColor);
e.Graphics.DrawString(text, e.Font, br, e.Bounds);
}
}
private void FillComboBoxWithEnum()
{
comboBox_syntaxOptions.Items.Clear();
foreach (string option in Enum.GetNames(typeof(HighlighterOptions)))
{
comboBox_syntaxOptions.Items.Add(option);
}
}
private void ComboBox_SyntaxOptions_SelectedIndexChanged(object sender, EventArgs e)
{
highlighterRules.SelectOption((HighlighterOptions)comboBox_syntaxOptions.SelectedIndex);
highlighterRules.HighlightSyntax();
}
private void Button_Open_Click(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
richTextBox_Editor.Text = File.ReadAllText(openFileDialog.FileName);
}
}
private void Button_Save_Click(object sender, EventArgs e)
{
var saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog.FileName, richTextBox_Editor.Text);
}
}
}
}