syncfusion-winforms-syntax-editor
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseImplementing Syntax Editors (EditControl)
实现语法编辑器(EditControl)
This skill guides the implementation of Syncfusion's EditControl, a powerful text editor control for creating interactive code editor applications with Visual Studio-like features including syntax highlighting, IntelliSense, code outlining, and comprehensive editing capabilities.
本指南介绍Syncfusion EditControl的实现方案,这是一款功能强大的文本编辑器控件,可用于构建具备类Visual Studio特性的交互式代码编辑器应用,支持语法高亮、IntelliSense、代码大纲以及全面的编辑能力。
When to Use This Skill
何时使用本指南
Use this skill when you need to:
- Create a code editor application with syntax highlighting for popular languages
- Build a text editor with Visual Studio-like editing features
- Implement syntax-aware code editing with IntelliSense support
- Add code outlining and folding capabilities to text editors
- Create custom language configurations for specialized syntax highlighting
- Build source code viewers or editors for multiple programming languages
- Implement advanced text editing with clipboard operations and undo/redo
- Add find and replace dialogs to text editing applications
- Create split-view code editors for viewing multiple sections simultaneously
- Export or print code with syntax highlighting preserved
- Implement auto-completion and auto-correction in text editors
- Build applications requiring line numbering, bookmarks, and code navigation
- Create text editors with rectangular block selection like Visual Studio
当你需要完成以下需求时可使用本指南:
- 创建支持主流编程语言语法高亮的代码编辑器应用
- 构建具备类Visual Studio编辑特性的文本编辑器
- 实现带IntelliSense支持的语法感知代码编辑功能
- 为文本编辑器添加代码大纲和折叠能力
- 为定制化语法高亮创建自定义语言配置
- 构建支持多编程语言的源代码查看器或编辑器
- 实现带剪贴板操作和撤销/重做功能的高级文本编辑能力
- 为文本编辑应用添加查找和替换对话框
- 创建分屏视图代码编辑器,支持同时查看文档的多个部分
- 导出或打印保留语法高亮样式的代码
- 在文本编辑器中实现自动补全和自动纠错功能
- 构建需要行号、书签和代码导航能力的应用
- 创建支持类Visual Studio矩形块选择功能的文本编辑器
Component Overview
组件概览
Control:
Namespace:
Assemblies Required:
EditControlNamespace:
Syncfusion.Windows.Forms.EditAssemblies Required:
- (primary)
Syncfusion.Edit.Windows.dll Syncfusion.Tools.Windows.dllSyncfusion.Shared.Base.dll
控件:
命名空间:
所需程序集:
EditControl命名空间:
Syncfusion.Windows.Forms.Edit所需程序集:
- (核心程序集)
Syncfusion.Edit.Windows.dll Syncfusion.Tools.Windows.dllSyncfusion.Shared.Base.dll
Key Capabilities
核心能力
The EditControl provides comprehensive code editing features:
- Syntax Highlighting: Built-in support for 12+ languages (C#, VB.NET, XML, HTML, Java, SQL, PowerShell, JavaScript, VBScript, Delphi, C, Text) plus custom language configuration via XML
- Editing Features: Clipboard operations, unlimited undo/redo, drag-and-drop, normal and rectangular block selection, change tracking with line modification markers
- IntelliSense: Auto-complete with predefined data, auto-correct for common typos, context tooltips for collapsed code blocks, custom IntelliSense popup
- Code Outlining: Expand/collapse code blocks with configurable outlining regions, bracket matching, collapsible sections
- Text Visualization: Line numbers, word wrap, current line highlighting, content dividers, underline styles (solid/dash/wave/dot), bookmark and custom indicators
- Navigation: Character, word, line, page, and document-level navigation with extensive API
- Find & Replace: Built-in dialogs with Visual Studio-like search and replace capabilities
- File Operations: Create, open, save with encoding support, export to XML/RTF/HTML, print with formatting
- Advanced Features: Split views for viewing multiple document sections, status bar with line/column/caret tracking, single-line mode for TextBox-like behavior, customizable shortcut keys
- Globalization: Complete localization support, RTL (right-to-left) text layout
- Events: Comprehensive event model for document changes, selection, navigation, and user interactions
EditControl提供全面的代码编辑特性:
- 语法高亮:内置支持12+种编程语言(C#、VB.NET、XML、HTML、Java、SQL、PowerShell、JavaScript、VBScript、Delphi、C、纯文本),同时支持通过XML自定义语言配置
- 编辑特性:剪贴板操作、无限次撤销/重做、拖放、常规和矩形块选择、带行修改标记的变更追踪
- IntelliSense:基于预定义数据的自动补全、常见输入错误自动纠正、折叠代码块的上下文提示、自定义IntelliSense弹窗
- 代码大纲:支持代码块展开/折叠,可配置大纲区域、括号匹配、可折叠段落
- 文本可视化:行号、自动换行、当前行高亮、内容分隔线、下划线样式(实线/虚线/波浪线/点线)、书签和自定义标记
- 导航能力:字符、单词、行、页和文档级别的导航API
- 查找与替换:内置对话框,支持类Visual Studio的搜索和替换能力
- 文件操作:支持编码的创建、打开、保存功能,导出为XML/RTF/HTML格式,带格式打印
- 高级特性:分屏视图支持查看文档的多个部分、带行/列/光标追踪的状态栏、类TextBox的单行模式、可自定义快捷键
- 全球化:完整的本地化支持、RTL(从右到左)文本布局
- 事件:完善的事件模型,支持文档变更、选择、导航和用户交互等场景
Documentation and Navigation Guide
文档和导航指南
Getting Started
入门指南
📄 Read: references/getting-started.md
When you need to:
- Install and reference EditControl assemblies
- Create EditControl via designer or code
- Configure basic properties (size, border, dock)
- Load initial content into the editor
- Set up a basic code editor application
📄 阅读: references/getting-started.md
适用场景:
- 安装并引用EditControl程序集
- 通过设计器或代码创建EditControl实例
- 配置基础属性(尺寸、边框、停靠方式)
- 为编辑器加载初始内容
- 搭建基础的代码编辑器应用
Syntax Highlighting
语法高亮
📄 Read: references/syntax-highlighting.md
When you need to:
- Apply built-in syntax highlighting for C#, VB.NET, XML, HTML, Java, SQL, PowerShell, JavaScript, etc.
- Use with
ApplyConfiguration()enumKnownLanguages - Create custom language configurations using XML
- Define lexems, formats, and code coloring rules
- Configure language-specific syntax elements
- Handle multiple language types in one application
📄 阅读: references/syntax-highlighting.md
适用场景:
- 为C#、VB.NET、XML、HTML、Java、SQL、PowerShell、JavaScript等语言应用内置语法高亮
- 结合枚举使用
KnownLanguages方法ApplyConfiguration() - 通过XML创建自定义语言配置
- 定义词法单元、格式和代码着色规则
- 配置语言专属的语法元素
- 在单个应用中处理多种语言类型
Editing Features
编辑特性
📄 Read: references/editing-features.md
When you need to:
- Implement clipboard operations (Cut, Copy, Paste)
- Configure unlimited undo/redo functionality
- Enable normal or rectangular block selection modes
- Support drag-and-drop text editing
- Add block indent and outdent capabilities
- Display line modification markers for change tracking
- Customize the context menu for editing operations
📄 阅读: references/editing-features.md
适用场景:
- 实现剪贴板操作(剪切、复制、粘贴)
- 配置无限次撤销/重做功能
- 开启常规或矩形块选择模式
- 支持拖放文本编辑
- 添加块缩进和取消缩进能力
- 显示行修改标记用于变更追踪
- 自定义编辑操作的右键菜单
IntelliSense Features
IntelliSense特性
📄 Read: references/intellisense.md
When you need to:
- Configure auto-complete with predefined data sources
- Enable auto-correct for common typos and misspellings
- Display context tooltips for collapsed code
- Create custom IntelliSense popups
- Handle IntelliSense events and user interactions
- Customize IntelliSense item appearance and behavior
📄 阅读: references/intellisense.md
适用场景:
- 基于预定义数据源配置自动补全
- 开启常见输入错误的自动纠正功能
- 为折叠代码显示上下文提示
- 创建自定义IntelliSense弹窗
- 处理IntelliSense事件和用户交互
- 自定义IntelliSense选项的外观和行为
Text Visualization
文本可视化
📄 Read: references/text-visualization.md
When you need to:
- Display line numbers with customization
- Enable code outlining and folding
- Configure word wrap behavior
- Highlight the current line with custom colors
- Add content dividers between sections
- Apply underline styles (solid, dash, wave, dot)
- Create bookmarks and custom indicators
- Navigate between bookmarks
📄 阅读: references/text-visualization.md
适用场景:
- 显示可自定义的行号
- 开启代码大纲和折叠功能
- 配置自动换行行为
- 用自定义颜色高亮当前行
- 在段落之间添加内容分隔线
- 应用下划线样式(实线、虚线、波浪线、点线)
- 创建书签和自定义标记
- 在书签之间导航
File Operations and Export
文件操作与导出
📄 Read: references/file-operations.md
When you need to:
- Create new documents programmatically
- Open and load files with
LoadFile() - Save documents with and
Save()SaveAs() - Handle file encoding (UTF-8, Unicode, ASCII)
- Export content to XML, RTF, or HTML formats
- Print documents with syntax highlighting
- Configure print settings and page setup
📄 阅读: references/file-operations.md
适用场景:
- 通过编程方式创建新文档
- 使用打开和加载文件
LoadFile() - 使用和
Save()保存文档SaveAs() - 处理文件编码(UTF-8、Unicode、ASCII)
- 将内容导出为XML、RTF或HTML格式
- 打印带语法高亮的文档
- 配置打印设置和页面布局
Advanced Features
高级特性
📄 Read: references/advanced-features.md
When you need to:
- Create split views for viewing multiple document sections
- Configure and customize the status bar
- Use single-line mode for TextBox-like behavior
- Implement find and replace dialogs
- Use text navigation methods programmatically
- Customize shortcut key bindings
- Enable RTL (right-to-left) support
- Localize the editor to different languages
- Handle comprehensive event model
- Optimize scrolling behavior for large files
📄 阅读: references/advanced-features.md
适用场景:
- 创建分屏视图以查看文档的多个部分
- 配置和自定义状态栏
- 使用单行模式实现类TextBox的行为
- 实现查找和替换对话框
- 通过编程方式使用文本导航方法
- 自定义快捷键绑定
- 开启RTL(从右到左)支持
- 将编辑器本地化为不同语言
- 处理完善的事件模型
- 优化大文件的滚动行为
Quick Start Example
快速入门示例
Basic Code Editor with C# Syntax Highlighting
具备C#语法高亮的基础代码编辑器
C#:
csharp
using Syncfusion.Windows.Forms.Edit;
using Syncfusion.Windows.Forms.Edit.Enums;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
public class CodeEditorForm : Form
{
private EditControl editControl1;
public CodeEditorForm()
{
InitializeComponent();
SetupCodeEditor();
}
private void SetupCodeEditor()
{
// Create EditControl
editControl1 = new EditControl();
// Basic configuration
editControl1.Dock = DockStyle.Fill;
editControl1.BorderStyle = BorderStyle.Fixed3D;
// Apply C# syntax highlighting
editControl1.ApplyConfiguration(KnownLanguages.CSharp);
// Enable line numbers
editControl1.ShowLineNumbers = true;
// Enable code outlining
editControl1.ShowOutliningCollapsers = true;
// Add to form
this.Controls.Add(editControl1);
// Load sample code (optional)
string sampleCode = @"using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""Hello, World!"");
}
}
}";
editControl1.Text = sampleCode;
}
private void InitializeComponent()
{
this.Text = "C# Code Editor";
this.Size = new Size(800, 600);
}
}VB.NET:
vb
Imports Syncfusion.Windows.Forms.Edit
Imports Syncfusion.Windows.Forms.Edit.Enums
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Public Class CodeEditorForm
Inherits Form
Private editControl1 As EditControl
Public Sub New()
InitializeComponent()
SetupCodeEditor()
End Sub
Private Sub SetupCodeEditor()
' Create EditControl
editControl1 = New EditControl()
' Basic configuration
editControl1.Dock = DockStyle.Fill
editControl1.BorderStyle = BorderStyle.Fixed3D
' Apply C# syntax highlighting
editControl1.ApplyConfiguration(KnownLanguages.CSharp)
' Enable line numbers
editControl1.ShowLineNumbers = True
' Enable code outlining
editControl1.ShowOutliningCollapsers = True
' Add to form
Me.Controls.Add(editControl1)
' Load sample code (optional)
Dim sampleCode As String = "using System;" & vbCrLf & vbCrLf & _
"namespace HelloWorld" & vbCrLf & _
"{" & vbCrLf & _
" class Program" & vbCrLf & _
" {" & vbCrLf & _
" static void Main(string[] args)" & vbCrLf & _
" {" & vbCrLf & _
" Console.WriteLine(""Hello, World!"");" & vbCrLf & _
" }" & vbCrLf & _
" }" & vbCrLf & _
"}"
editControl1.Text = sampleCode
End Sub
Private Sub InitializeComponent()
Me.Text = "C# Code Editor"
Me.Size = New Size(800, 600)
End Sub
End ClassC#:
csharp
using Syncfusion.Windows.Forms.Edit;
using Syncfusion.Windows.Forms.Edit.Enums;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
public class CodeEditorForm : Form
{
private EditControl editControl1;
public CodeEditorForm()
{
InitializeComponent();
SetupCodeEditor();
}
private void SetupCodeEditor()
{
// Create EditControl
editControl1 = new EditControl();
// Basic configuration
editControl1.Dock = DockStyle.Fill;
editControl1.BorderStyle = BorderStyle.Fixed3D;
// Apply C# syntax highlighting
editControl1.ApplyConfiguration(KnownLanguages.CSharp);
// Enable line numbers
editControl1.ShowLineNumbers = true;
// Enable code outlining
editControl1.ShowOutliningCollapsers = true;
// Add to form
this.Controls.Add(editControl1);
// Load sample code (optional)
string sampleCode = @"using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""Hello, World!"");
}
}
}";
editControl1.Text = sampleCode;
}
private void InitializeComponent()
{
this.Text = "C# Code Editor";
this.Size = new Size(800, 600);
}
}VB.NET:
vb
Imports Syncfusion.Windows.Forms.Edit
Imports Syncfusion.Windows.Forms.Edit.Enums
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Public Class CodeEditorForm
Inherits Form
Private editControl1 As EditControl
Public Sub New()
InitializeComponent()
SetupCodeEditor()
End Sub
Private Sub SetupCodeEditor()
' Create EditControl
editControl1 = New EditControl()
' Basic configuration
editControl1.Dock = DockStyle.Fill
editControl1.BorderStyle = BorderStyle.Fixed3D
' Apply C# syntax highlighting
editControl1.ApplyConfiguration(KnownLanguages.CSharp)
' Enable line numbers
editControl1.ShowLineNumbers = True
' Enable code outlining
editControl1.ShowOutliningCollapsers = True
' Add to form
Me.Controls.Add(editControl1)
' Load sample code (optional)
Dim sampleCode As String = "using System;" & vbCrLf & vbCrLf & _
"namespace HelloWorld" & vbCrLf & _
"{" & vbCrLf & _
" class Program" & vbCrLf & _
" {" & vbCrLf & _
" static void Main(string[] args)" & vbCrLf & _
" {" & vbCrLf & _
" Console.WriteLine(""Hello, World!"");" & vbCrLf & _
" }" & vbCrLf & _
" }" & vbCrLf & _
"}"
editControl1.Text = sampleCode
End Sub
Private Sub InitializeComponent()
Me.Text = "C# Code Editor"
Me.Size = New Size(800, 600)
End Sub
End ClassCommon Patterns
常用模式
Pattern 1: Multi-Language Code Editor
模式1:多语言代码编辑器
Create an editor that switches between different programming languages:
C#:
csharp
private ComboBox languageSelector;
private EditControl editControl1;
private void SetupMultiLanguageEditor()
{
// Language selector
languageSelector = new ComboBox
{
Dock = DockStyle.Top,
DropDownStyle = ComboBoxStyle.DropDownList
};
languageSelector.Items.AddRange(new object[]
{
"C#", "VB.NET", "XML", "HTML", "Java", "SQL", "PowerShell", "JavaScript"
});
languageSelector.SelectedIndex = 0;
languageSelector.SelectedIndexChanged += LanguageSelector_SelectedIndexChanged;
// Editor
editControl1 = new EditControl
{
Dock = DockStyle.Fill,
ShowLineNumbers = true,
ShowOutliningCollapsers = true
};
this.Controls.Add(editControl1);
this.Controls.Add(languageSelector);
// Apply initial language
ApplyLanguage("C#");
}
private void LanguageSelector_SelectedIndexChanged(object sender, EventArgs e)
{
ApplyLanguage(languageSelector.SelectedItem.ToString());
}
private void ApplyLanguage(string language)
{
switch (language)
{
case "C#":
editControl1.ApplyConfiguration(KnownLanguages.CSharp);
break;
case "VB.NET":
editControl1.ApplyConfiguration(KnownLanguages.VB);
break;
case "XML":
editControl1.ApplyConfiguration(KnownLanguages.XML);
break;
case "HTML":
editControl1.ApplyConfiguration(KnownLanguages.HTML);
break;
case "Java":
editControl1.ApplyConfiguration(KnownLanguages.Java);
break;
case "SQL":
editControl1.ApplyConfiguration(KnownLanguages.SQL);
break;
case "PowerShell":
editControl1.ApplyConfiguration(KnownLanguages.PowerShell);
break;
case "JavaScript":
editControl1.ApplyConfiguration(KnownLanguages.JScript);
break;
}
}创建可在不同编程语言之间切换的编辑器:
C#:
csharp
private ComboBox languageSelector;
private EditControl editControl1;
private void SetupMultiLanguageEditor()
{
// Language selector
languageSelector = new ComboBox
{
Dock = DockStyle.Top,
DropDownStyle = ComboBoxStyle.DropDownList
};
languageSelector.Items.AddRange(new object[]
{
"C#", "VB.NET", "XML", "HTML", "Java", "SQL", "PowerShell", "JavaScript"
});
languageSelector.SelectedIndex = 0;
languageSelector.SelectedIndexChanged += LanguageSelector_SelectedIndexChanged;
// Editor
editControl1 = new EditControl
{
Dock = DockStyle.Fill,
ShowLineNumbers = true,
ShowOutliningCollapsers = true
};
this.Controls.Add(editControl1);
this.Controls.Add(languageSelector);
// Apply initial language
ApplyLanguage("C#");
}
private void LanguageSelector_SelectedIndexChanged(object sender, EventArgs e)
{
ApplyLanguage(languageSelector.SelectedItem.ToString());
}
private void ApplyLanguage(string language)
{
switch (language)
{
case "C#":
editControl1.ApplyConfiguration(KnownLanguages.CSharp);
break;
case "VB.NET":
editControl1.ApplyConfiguration(KnownLanguages.VB);
break;
case "XML":
editControl1.ApplyConfiguration(KnownLanguages.XML);
break;
case "HTML":
editControl1.ApplyConfiguration(KnownLanguages.HTML);
break;
case "Java":
editControl1.ApplyConfiguration(KnownLanguages.Java);
break;
case "SQL":
editControl1.ApplyConfiguration(KnownLanguages.SQL);
break;
case "PowerShell":
editControl1.ApplyConfiguration(KnownLanguages.PowerShell);
break;
case "JavaScript":
editControl1.ApplyConfiguration(KnownLanguages.JScript);
break;
}
}Pattern 2: File Editor with Save/Load
模式2:带保存/加载功能的文件编辑器
Create an editor with file operations:
C#:
csharp
private EditControl editControl1;
private string currentFilePath = null;
private void SetupFileEditor()
{
editControl1 = new EditControl
{
Dock = DockStyle.Fill,
ShowLineNumbers = true
};
// Menu bar
MenuStrip menuStrip = new MenuStrip();
ToolStripMenuItem fileMenu = new ToolStripMenuItem("File");
fileMenu.DropDownItems.Add("New", null, NewFile_Click);
fileMenu.DropDownItems.Add("Open...", null, OpenFile_Click);
fileMenu.DropDownItems.Add("Save", null, SaveFile_Click);
fileMenu.DropDownItems.Add("Save As...", null, SaveFileAs_Click);
menuStrip.Items.Add(fileMenu);
this.Controls.Add(editControl1);
this.Controls.Add(menuStrip);
this.MainMenuStrip = menuStrip;
}
private void NewFile_Click(object sender, EventArgs e)
{
editControl1.Text = string.Empty;
currentFilePath = null;
this.Text = "Untitled - Code Editor";
}
private void OpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog
{
Filter = "All Files (*.*)|*.*|C# Files (*.cs)|*.cs|VB Files (*.vb)|*.vb"
};
if (openDialog.ShowDialog() == DialogResult.OK)
{
editControl1.LoadFile(openDialog.FileName);
currentFilePath = openDialog.FileName;
this.Text = Path.GetFileName(currentFilePath) + " - Code Editor";
// Auto-detect language based on extension
string ext = Path.GetExtension(currentFilePath).ToLower();
if (ext == ".cs")
editControl1.ApplyConfiguration(KnownLanguages.CSharp);
else if (ext == ".vb")
editControl1.ApplyConfiguration(KnownLanguages.VB);
else if (ext == ".xml")
editControl1.ApplyConfiguration(KnownLanguages.XML);
}
}
private void SaveFile_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(currentFilePath))
{
SaveFileAs_Click(sender, e);
}
else
{
editControl1.Save(currentFilePath);
}
}
private void SaveFileAs_Click(object sender, EventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog
{
Filter = "All Files (*.*)|*.*|C# Files (*.cs)|*.cs|VB Files (*.vb)|*.vb"
};
if (saveDialog.ShowDialog() == DialogResult.OK)
{
editControl1.Save(saveDialog.FileName);
currentFilePath = saveDialog.FileName;
this.Text = Path.GetFileName(currentFilePath) + " - Code Editor";
}
}创建支持文件操作的编辑器:
C#:
csharp
private EditControl editControl1;
private string currentFilePath = null;
private void SetupFileEditor()
{
editControl1 = new EditControl
{
Dock = DockStyle.Fill,
ShowLineNumbers = true
};
// Menu bar
MenuStrip menuStrip = new MenuStrip();
ToolStripMenuItem fileMenu = new ToolStripMenuItem("File");
fileMenu.DropDownItems.Add("New", null, NewFile_Click);
fileMenu.DropDownItems.Add("Open...", null, OpenFile_Click);
fileMenu.DropDownItems.Add("Save", null, SaveFile_Click);
fileMenu.DropDownItems.Add("Save As...", null, SaveFileAs_Click);
menuStrip.Items.Add(fileMenu);
this.Controls.Add(editControl1);
this.Controls.Add(menuStrip);
this.MainMenuStrip = menuStrip;
}
private void NewFile_Click(object sender, EventArgs e)
{
editControl1.Text = string.Empty;
currentFilePath = null;
this.Text = "Untitled - Code Editor";
}
private void OpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog
{
Filter = "All Files (*.*)|*.*|C# Files (*.cs)|*.cs|VB Files (*.vb)|*.vb"
};
if (openDialog.ShowDialog() == DialogResult.OK)
{
editControl1.LoadFile(openDialog.FileName);
currentFilePath = openDialog.FileName;
this.Text = Path.GetFileName(currentFilePath) + " - Code Editor";
// Auto-detect language based on extension
string ext = Path.GetExtension(currentFilePath).ToLower();
if (ext == ".cs")
editControl1.ApplyConfiguration(KnownLanguages.CSharp);
else if (ext == ".vb")
editControl1.ApplyConfiguration(KnownLanguages.VB);
else if (ext == ".xml")
editControl1.ApplyConfiguration(KnownLanguages.XML);
}
}
private void SaveFile_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(currentFilePath))
{
SaveFileAs_Click(sender, e);
}
else
{
editControl1.Save(currentFilePath);
}
}
private void SaveFileAs_Click(object sender, EventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog
{
Filter = "All Files (*.*)|*.*|C# Files (*.cs)|*.cs|VB Files (*.vb)|*.vb"
};
if (saveDialog.ShowDialog() == DialogResult.OK)
{
editControl1.Save(saveDialog.FileName);
currentFilePath = saveDialog.FileName;
this.Text = Path.GetFileName(currentFilePath) + " - Code Editor";
}
}Pattern 3: Code Editor with IntelliSense
模式3:带IntelliSense的代码编辑器
C#:
csharp
editControl1 = new EditControl { Dock = DockStyle.Fill, ShowLineNumbers = true };
editControl1.ApplyConfiguration(KnownLanguages.CSharp);
// Add auto-complete items
editControl1.AutoCompleteList.AddRange(new[] { "Console", "Console.WriteLine",
"string", "int", "public", "private", "class" });
// Enable auto-correct
editControl1.AutoCorrectList.Add("teh", "the");
editControl1.AutoCorrectList.Add("cosole", "console");
this.Controls.Add(editControl1);C#:
csharp
editControl1 = new EditControl { Dock = DockStyle.Fill, ShowLineNumbers = true };
editControl1.ApplyConfiguration(KnownLanguages.CSharp);
// Add auto-complete items
editControl1.AutoCompleteList.AddRange(new[] { "Console", "Console.WriteLine",
"string", "int", "public", "private", "class" });
// Enable auto-correct
editControl1.AutoCorrectList.Add("teh", "the");
editControl1.AutoCorrectList.Add("cosole", "console");
this.Controls.Add(editControl1);Key Properties and Methods
核心属性和方法
Essential Properties
基础属性
| Property | Type | Description |
|---|---|---|
| Text | | Gets or sets the text content of the editor |
| ShowLineNumbers | | Shows or hides line numbers in the margin |
| ShowOutliningCollapsers | | Enables code outlining collapse/expand controls |
| WordWrap | | Enables or disables word wrapping |
| ReadOnly | | Makes the editor read-only |
| TabSize | | Sets the number of spaces for tab character |
| AutoCompleteMode | | Configures auto-complete behavior (All, Suggest, None) |
| EnableAutoCorrect | | Enables automatic correction of common typos |
| StatusBarSettings | | Configures the built-in status bar |
| ContextChoiceOpen | | Gets whether IntelliSense context menu is open |
| CanUndo | | Indicates if undo operation is available |
| CanRedo | | Indicates if redo operation is available |
| Modified | | Indicates if the document has been modified |
| 属性 | 类型 | 描述 |
|---|---|---|
| Text | | 获取或设置编辑器的文本内容 |
| ShowLineNumbers | | 显示或隐藏边距中的行号 |
| ShowOutliningCollapsers | | 开启代码大纲折叠/展开控件 |
| WordWrap | | 开启或关闭自动换行 |
| ReadOnly | | 将编辑器设为只读模式 |
| TabSize | | 设置制表符对应的空格数 |
| AutoCompleteMode | | 配置自动补全行为(All、Suggest、None) |
| EnableAutoCorrect | | 开启常见输入错误的自动纠正功能 |
| StatusBarSettings | | 配置内置状态栏 |
| ContextChoiceOpen | | 获取IntelliSense上下文菜单是否处于打开状态 |
| CanUndo | | 指示是否可以执行撤销操作 |
| CanRedo | | 指示是否可以执行重做操作 |
| Modified | | 指示文档是否已被修改 |
Key Methods
核心方法
| Method | Description |
|---|---|
| ApplyConfiguration(KnownLanguages) | Applies built-in syntax highlighting configuration |
| LoadFile(string) | Loads a file into the editor |
| Save(string) | Saves the current content to a file |
| SaveAs(string) | Saves content to a new file |
| Undo() | Performs undo operation |
| Redo() | Performs redo operation |
| Cut() | Cuts selected text to clipboard |
| Copy() | Copies selected text to clipboard |
| Paste() | Pastes text from clipboard |
| SelectAll() | Selects all text in the editor |
| Find() | Opens the find dialog |
| Replace() | Opens the replace dialog |
| GoToLine(int) | Navigates to specified line number |
| SetText(string) | Sets text content programmatically |
| Export(string, ExportType) | Exports content to XML, RTF, or HTML |
| Print() | Opens print dialog for the document |
| 方法 | 描述 |
|---|---|
| ApplyConfiguration(KnownLanguages) | 应用内置的语法高亮配置 |
| LoadFile(string) | 将文件加载到编辑器中 |
| Save(string) | 将当前内容保存到文件 |
| SaveAs(string) | 将内容保存到新文件 |
| Undo() | 执行撤销操作 |
| Redo() | 执行重做操作 |
| Cut() | 将选中的文本剪切到剪贴板 |
| Copy() | 将选中的文本复制到剪贴板 |
| Paste() | 从剪贴板粘贴文本 |
| SelectAll() | 选中编辑器中的所有文本 |
| Find() | 打开查找对话框 |
| Replace() | 打开替换对话框 |
| GoToLine(int) | 导航到指定行号 |
| SetText(string) | 通过编程方式设置文本内容 |
| Export(string, ExportType) | 将内容导出为XML、RTF或HTML格式 |
| Print() | 打开文档的打印对话框 |
Common Use Cases
常见用例
- Source Code Editor: Build Visual Studio-like code editors for C#, VB.NET, Java with syntax highlighting and IntelliSense
- Configuration File Editor: XML, JSON editors with syntax validation
- SQL Query Editor: Database query editors with SQL syntax highlighting
- Script Editor: PowerShell, JavaScript editors for automation
- Code Snippet Manager: Store and organize code snippets with syntax highlighting
- Log File Viewer: View structured logs with syntax highlighting
- Multi-Language IDE: Build IDEs supporting multiple languages with split views
- 源代码编辑器:为C#、VB.NET、Java构建类Visual Studio的代码编辑器,支持语法高亮和IntelliSense
- 配置文件编辑器:带语法校验的XML、JSON编辑器
- SQL查询编辑器:支持SQL语法高亮的数据库查询编辑器
- 脚本编辑器:用于自动化场景的PowerShell、JavaScript编辑器
- 代码片段管理器:存储和管理带语法高亮的代码片段
- 日志文件查看器:查看带语法高亮的结构化日志
- 多语言IDE:构建支持多语言、带分屏视图的集成开发环境
Next Steps
后续步骤
Start with Getting Started to install and configure the EditControl, then explore specific features through the navigation guide above based on your application requirements.
从**入门指南**开始安装和配置EditControl,然后根据你的应用需求,通过上面的导航指南探索对应的特性。