Public Sub IC_TextChanged(ByVal sender As ICSharpCode.TextEditor.TextEditorControl, ByVal e As System.EventArgs) '把对象传递到自定义内部函数维护,这样改功能,就不需要重启项目 Functions.Execute("Handle_IC_TextChanged", sender) End Sub
'当代码编辑器TextChanged后触发,可以通过Name来判断是哪个代码编辑器的事件 Dim e As ICSharpCode.TextEditor.TextEditorControl = Args(0) 'MessageBox.Show(e.Name & "监听到变化:" & e.Text )
Dim ct As New ICSharpCode.TextEditor.TextEditorControl ct.Dock = System.Windows.Forms.DockStyle.Fill '填充整个Panel ct.Name = "TextEditor" '控件名称 ct.Font = New Font("宋体", 10) '字体设置 ct.Encoding = System.Text.Encoding.Default '使用系统默认编码方式 AddHandler ct.TextChanged , AddressOf IC_TextChanged '添加TextChanged变化的事件监听 ct.ShowEOLMarkers = False '显示换行光标,推荐false ct.ShowHRuler = True '显示上标尺,推荐true ct.ShowInvalidLines = False '显示无用空行的标志,推荐false ct.ShowMatchingBracket = True '显示匹配括号 ct.ShowSpaces = False '显示空格符 ct.ShowTabs = True '显示缩进符 ct.ShowVRuler = False '显示垂直虚拟尺子 ct.ShowHRuler = False '显示水平虚拟尺子 ct.AllowCaretBeyondEOL = False '是否允许直接在空白处开始编辑 ct.ConvertTabsToSpaces = True '用空格符替换tab效果 ct.SetHighlighting("VBNET") '设置当前高亮语法规则 '有这些规则:ASP/XHTML,BAT,Boo,Coco,CSS,C++.NET,C#,HTML,Java,JavaScript,JSON,Patch,PHP,SQL,TeX,VBNET,XML Dim pan As WinForm.Panel = e.Form.Controls("Panel1") pan.BaseControl.Controls.Add(ct)
Dim ct As ICSharpCode.TextEditor.TextEditorControl = e.Form.Controls("Panel1").BaseControl.controls("TextEditor") ct.Dispose '释放内存
Dim ct As ICSharpCode.TextEditor.TextEditorControl = e.Form.Controls("Panel1").BaseControl.controls("TextEditor") Dim Code As String Code = "Dim Val1 As Integer = 1" & vbcrlf Code = Code & "Dim Val2 As Integer = 2" & vbcrlf Code = Code & "Dim Sum As Integer = Val1 + Val2" & vbcrlf Code = Code & "Return Sum" ct.Text = Code ct.Refresh '刷新一下,有时候会渲染延迟
Dim ct As ICSharpCode.TextEditor.TextEditorControl = e.Form.Controls("Panel1").BaseControl.controls("TextEditor") MessageBox.Show(ct.Text)
Dim synDir As String = ProjectPath & "Attachments\Syntax" Dim ct As ICSharpCode.TextEditor.TextEditorControl = e.Form.Controls("Panel1").BaseControl.controls("TextEditor") Dim cb1 As WinForm.ComboBox = e.Form.Controls("ComboBox_语法颜色") If cb1.Text <> "" Then If FileSys.DirectoryExists(synDir) Then Dim fsmProvider = New ICSharpCode.TextEditor.Document.FileSyntaxModeProvider(synDir) ICSharpCode.TextEditor.Document.HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmProvider) ct.SetHighlighting(cb1.Text) Else MessageBox.Show(synDir & "不存在") End If Else MessageBox.Show("请先选择语法") End If这样你每次改了语法文件,直接点这个应用按钮,就立刻生效了
Dim ct As ICSharpCode.TextEditor.TextEditorControl = e.Form.Controls("Panel1").BaseControl.controls("TextEditor") MessageBox.Show(ct.Text)
有的人代码会报以下错误,一般是在做了字符串截取后
解决方法:代码处理的结尾增加.TrimEnd(chr(13),例如
If s.EndsWith("e.Handled = True") = True Then s = s.SubString(0, s.Length - 17).TrimEnd(chr(13)) '要同时去掉回车符/r,不然会多了个奇怪换行让IC编辑器报错 End If
6完整案例下载
链接:https://pan.baidu.com/s/1bKKYZ1X0AIv0JOra5Uumbw?pwd=m51q
提取码:m51q