Provides a custom control that facilitates the usage of the control ICSharpCode.TextEditor.TextEditorControl.
$ dotnet add package ZidUtilities.CommonCode.ICSharpTextEditorExtended text editor control based on ICSharpCode.TextEditor with advanced syntax highlighting and editing features.
By default this control provides syntax highlight for a good number of the languages, the syntax is based on the xshd files provided by this github repository: https://github.com/xv/ICSharpCode.TextEditor-Lexers Except for TransactSQL which has been custom made for this library.
To use the syntax highlighting features you need to set the Syntax property of the ExtendedEditor control to one of the predefined syntax types.
The control also provides code folding and bracket matching depending on the language selected.
Also there is a toolbar with expected functionality, comment, uncomment, toggle bookmark, etc. to access to this features search for the
preperties type ToolbarOption in the ExtendedEditor control, you can work with them on the designer.
Buttons Run, Stop and Kill, do not provide functionality by default, you need to handle them using the control's events: OnRun, OnStop and OnKill.
The toolbar can be also customized with shortcuts, look at the class ToolbarOption and its properties ShortCut and ThenShortCut.
Finally the control also provide implicit shortcuts for common operations, selection to uppercase, lowercase, etc. Look at the class ImplicitShortcut for more information.
The original ICSharpCode.TextEditor.TextEditorControl can be accessed through the propety Editor of the ExtendedEditor control.
These functionlity satisfy most of MY needs, I might come back and change or add more features in the future, but for now this is it.
.NET Framework 4.8
Add a reference to CommonCode.ICSharpTextEditor.dll in your Windows Forms project. The ICSharpCode.TextEditor component is included.
using ZidUtilities.CommonCode.ICSharpTextEditor;
// Add ExtendedEditor to your form
var codeEditor = new ExtendedEditor
{
Dock = DockStyle.Fill,
ShowLineNumbers = true
};
this.Controls.Add(codeEditor);using ICSharpCode.TextEditor.Document;
// Set syntax highlighting based on file extension
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.CSharp;
// Set highlighting dynamically
string extension = Path.GetExtension(fileName).ToLower();
switch (extension)
{
case ".cs":
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.CSharp;
break;
case ".vb":
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.VBNET;
break;
case ".xml":
case ".config":
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.XML;
break;
case ".html":
case ".htm":
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.HTML;
break;
case ".js":
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.JavaScript;
break;
case ".java":
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.Java;
break;
case ".cpp":
case ".h":
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.CPlusPlus;
break;
case ".sql":
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.TransactSQL;
break;
default:
extendedEditor.Syntax = ZidUtilities.CommonCode.ICSharpTextEditor.SyntaxHighlighting.None;
break;
}// Get selected text
string selectedText = codeEditor.ActiveTextAreaControl.SelectionManager.SelectedText;
// Replace selected text
codeEditor.ActiveTextAreaControl.SelectionManager.SelectedText = "replacement";
// Select all
codeEditor.ActiveTextAreaControl.SelectAll();
// Get current line
int lineNumber = codeEditor.ActiveTextAreaControl.Caret.Line;
string lineText = codeEditor.Document.GetText(codeEditor.Document.GetLineSegment(lineNumber));
// Insert text at caret
int offset = codeEditor.ActiveTextAreaControl.Caret.Offset;
codeEditor.Document.Insert(offset, "inserted text");
// Clear all text
codeEditor.Text = string.Empty;Use the provide search functionality in the toolbar or implement custom search using the editor's document methods.
public class SimpleCodeEditor : Form
{
private ExtendedEditor codeEditor;
private string currentFile;
public SimpleCodeEditor()
{
InitializeComponents();
}
private void InitializeComponents()
{
this.Text = "Simple Code Editor";
this.Size = new Size(800, 600);
// Create editor
//Dont do this, use visual studio designer to add the control to the form, makes everythign easier
//codeEditor = new ExtendedEditor
//{
// Dock = DockStyle.Fill,
// ShowLineNumbers = true
//};
//By default open file functionality is included in the toolbar
//var openItem = new ToolStripMenuItem("Open", null, OpenFile_Click);
//By default save file functionality is included in the toolbar
//var saveItem = new ToolStripMenuItem("Save", null, SaveFile_Click);
}
}This library is specifically for Windows Forms applications. For WPF applications, use CommonCode.AvalonEdit instead.