forked from PavelTorgashov/FastColoredTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerfulSample.vb
More file actions
143 lines (122 loc) · 7 KB
/
Copy pathPowerfulSample.vb
File metadata and controls
143 lines (122 loc) · 7 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
Imports System.Text.RegularExpressions
Imports System.IO
Imports FastColoredTextBoxNS
''' <summary>
''' Note: this example is deprecated. See project Tester for newest code samples.
''' </summary>
Public Class PowerfulSample
Dim lang As String = "VB"
Dim BlueStyle As TextStyle = New TextStyle(Brushes.Blue, Nothing, FontStyle.Regular)
Dim BoldStyle As TextStyle = New TextStyle(Nothing, Nothing, FontStyle.Bold Or FontStyle.Underline)
Dim GrayStyle As TextStyle = New TextStyle(Brushes.Gray, Nothing, FontStyle.Regular)
Dim MagentaStyle As TextStyle = New TextStyle(Brushes.Magenta, Nothing, FontStyle.Regular)
Dim GreenStyle As TextStyle = New TextStyle(Brushes.Green, Nothing, FontStyle.Italic)
Dim BrownStyle As TextStyle = New TextStyle(Brushes.Brown, Nothing, FontStyle.Italic)
Dim MaroonStyle As TextStyle = New TextStyle(Brushes.Maroon, Nothing, FontStyle.Regular)
Dim SameWordsStyle As MarkerStyle = New MarkerStyle(New SolidBrush(Color.FromArgb(40, Color.Gray)))
Public Sub New()
InitializeComponent()
InitStylesPriority()
End Sub
Private Sub InitStylesPriority()
FastColoredTextBox1.ClearStylesBuffer()
'add this style explicitly for drawing under other styles
FastColoredTextBox1.AddStyle(SameWordsStyle)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FastColoredTextBox1.Text = "" & vbCrLf &
"#Region ""Char""" & vbCrLf &
" " & vbCrLf &
" ''' <summary>" & vbCrLf &
" ''' Char and style" & vbCrLf &
" ''' </summary>" & vbCrLf &
" Public Structure CharStyle" & vbCrLf &
" Public c As Char" & vbCrLf &
" Public style As StyleIndex" & vbCrLf &
" " & vbCrLf &
" Public Sub CharStyle(ByVal ch As Char)" & vbCrLf &
" c = ch" & vbCrLf &
" Style = StyleIndex.None" & vbCrLf &
" End Sub" & vbCrLf &
" " & vbCrLf &
" End Structure" & vbCrLf &
" " & vbCrLf &
"#End Region"
'move caret to start text
FastColoredTextBox1.Selection.Start = Place.Empty
FastColoredTextBox1.DoCaretVisible()
FastColoredTextBox1.IsChanged = False
FastColoredTextBox1.ClearUndo()
End Sub
Private Sub FastColoredTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As FastColoredTextBoxNS.TextChangedEventArgs) Handles FastColoredTextBox1.TextChanged
'For sample, we will highlight the syntax of C# manually, although could use built-in highlighter
If lang = "CSharp" Then
CSharpSyntaxHighlight(e)
End If
End Sub
Private Sub CSharpSyntaxHighlight(ByVal e As TextChangedEventArgs)
FastColoredTextBox1.LeftBracket = "("
FastColoredTextBox1.RightBracket = ")"
FastColoredTextBox1.LeftBracket2 = "\x0"
FastColoredTextBox1.RightBracket2 = "\x0"
'clear style of changed range
e.ChangedRange.ClearStyle(BlueStyle, BoldStyle, GrayStyle, MagentaStyle, GreenStyle, BrownStyle)
'string highlighting
e.ChangedRange.SetStyle(BrownStyle, """.*?""|'.+?'")
'comment highlighting
e.ChangedRange.SetStyle(GreenStyle, "//.*$", RegexOptions.Multiline)
e.ChangedRange.SetStyle(GreenStyle, "(/\*.*?\*/)|(/\*.*)", RegexOptions.Singleline)
e.ChangedRange.SetStyle(GreenStyle, "(/\*.*?\*/)|(.*\*/)", RegexOptions.Singleline Or RegexOptions.RightToLeft)
'number highlighting
e.ChangedRange.SetStyle(MagentaStyle, "\b\d+[\.]?\d*([eE]\-?\d+)?[lLdDfF]?\b|\b0x[a-fA-F\d]+\b")
'attribute highlighting
e.ChangedRange.SetStyle(GrayStyle, "^\s*(?<range>\[.+?\])\s*$", RegexOptions.Multiline)
'class name highlighting
e.ChangedRange.SetStyle(BoldStyle, "\b(class|struct|enum|interface)\s+(?<range>\w+?)\b")
'keyword highlighting
e.ChangedRange.SetStyle(BlueStyle, "\b(abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|void|volatile|while|add|alias|ascending|descending|dynamic|from|get|global|group|into|join|let|orderby|partial|remove|select|set|value|var|where|yield)\b|#region\b|#endregion\b")
'clear folding markers
e.ChangedRange.ClearFoldingMarkers()
'set folding markers
e.ChangedRange.SetFoldingMarkers("{", "}") 'allow to collapse brackets block
e.ChangedRange.SetFoldingMarkers("#region\b", "#endregion\b") 'allow to collapse #region blocks
e.ChangedRange.SetFoldingMarkers("/\*", "\*/") 'allow to collapse comment block
End Sub
Private Sub miCSharp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miCSharp.Click, sQLToolStripMenuItem.Click, pHPToolStripMenuItem.Click, miVB.Click, hTMLToolStripMenuItem.Click
'set language
lang = CType(sender, ToolStripMenuItem).Text
FastColoredTextBox1.ClearStylesBuffer()
FastColoredTextBox1.Range.ClearStyle(StyleIndex.All)
InitStylesPriority()
Select Case lang
Case "CSharp"
FastColoredTextBox1.Language = Language.Custom
FastColoredTextBox1.CommentPrefix = "//"
'call OnTextChanged for refresh syntax highlighting
FastColoredTextBox1.OnTextChanged()
Case "VB" : FastColoredTextBox1.Language = Language.VB
Case "HTML" : FastColoredTextBox1.Language = Language.HTML
Case "SQL" : FastColoredTextBox1.Language = Language.SQL
Case "PHP" : FastColoredTextBox1.Language = Language.PHP
End Select
FastColoredTextBox1.OnSyntaxHighlight(New TextChangedEventArgs(FastColoredTextBox1.Range))
End Sub
Private Sub miLanguage_DropDownOpening(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miLanguage.DropDownOpening
For Each mi As ToolStripMenuItem In miLanguage.DropDownItems
mi.Checked = (mi.Text = lang)
Next
End Sub
Private Sub findToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles findToolStripMenuItem.Click
FastColoredTextBox1.ShowFindDialog()
End Sub
Private Sub replaceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles replaceToolStripMenuItem.Click
FastColoredTextBox1.ShowReplaceDialog()
End Sub
Private Sub hTMLToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hTMLToolStripMenuItem1.Click
Dim sfd As SaveFileDialog = New SaveFileDialog()
sfd.Filter = "HTML|*.html"
If sfd.ShowDialog() = DialogResult.OK Then
File.WriteAllText(sfd.FileName, FastColoredTextBox1.Html)
End If
End Sub
End Class