forked from PavelTorgashov/FastColoredTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleSample.cs
More file actions
158 lines (136 loc) · 3.95 KB
/
Copy pathConsoleSample.cs
File metadata and controls
158 lines (136 loc) · 3.95 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using FastColoredTextBoxNS;
namespace Tester
{
public partial class ConsoleSample : Form
{
private bool stop;
public ConsoleSample()
{
InitializeComponent();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
string text = "";
stop = false;
do
{
consoleTextBox1.WriteLine("Enter some line: ");
text = consoleTextBox1.ReadLine();
} while (text != "" && !stop);
consoleTextBox1.WriteLine("End of enetering.");
}
protected override void OnClosing(CancelEventArgs e)
{
Stop();
base.OnClosing(e);
}
void Stop()
{
stop = true;
consoleTextBox1.IsReadLineMode = false;
}
}
/// <summary>
/// Console emulator.
/// </summary>
public class ConsoleTextBox : FastColoredTextBox
{
private volatile bool isReadLineMode;
private volatile bool isUpdating;
private Place StartReadPlace { get; set; }
/// <summary>
/// Control is waiting for line entering.
/// </summary>
public bool IsReadLineMode
{
get { return isReadLineMode; }
set { isReadLineMode = value; }
}
/// <summary>
/// Append line to end of text.
/// </summary>
/// <param name="text"></param>
public void WriteLine(string text)
{
IsReadLineMode = false;
isUpdating = true;
try
{
AppendText(text);
GoEnd();
}
finally
{
isUpdating = false;
ClearUndo();
}
}
/// <summary>
/// Wait for line entering.
/// Set IsReadLineMode to false for break of waiting.
/// </summary>
/// <returns></returns>
public string ReadLine()
{
GoEnd();
StartReadPlace = Range.End;
IsReadLineMode = true;
try
{
while (IsReadLineMode)
{
Application.DoEvents();
Thread.Sleep(5);
}
}
finally
{
IsReadLineMode = false;
ClearUndo();
}
return new Range(this, StartReadPlace, Range.End).Text.TrimEnd('\r', '\n');
}
public override void OnTextChanging(ref string text)
{
if (!IsReadLineMode && !isUpdating)
{
text = ""; //cancel changing
return;
}
if (IsReadLineMode)
{
if (Selection.Start < StartReadPlace || Selection.End < StartReadPlace)
GoEnd();//move caret to entering position
if (Selection.Start == StartReadPlace || Selection.End == StartReadPlace)
if (text == "\b") //backspace
{
text = ""; //cancel deleting of last char of readonly text
return;
}
if (text != null && text.Contains('\n'))
{
text = text.Substring(0, text.IndexOf('\n') + 1);
IsReadLineMode = false;
}
}
base.OnTextChanging(ref text);
}
public override void Clear()
{
var oldIsReadMode = isReadLineMode;
isReadLineMode = false;
isUpdating = true;
base.Clear();
isUpdating = false;
isReadLineMode = oldIsReadMode;
StartReadPlace = Place.Empty;
}
}
}