Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the .NET Code Editing Control ICSharpCode.TextEditor

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use. NET code editing control ICSharpCode.TextEditor. It has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

Let's look at the effect of operation:

I. Project Structure

Here you need to pay attention to the lib folder imported library, this Demo needs these dll.

II. Code folding

You need to implement the GenerateFolders method in IFoldingStrategy, as follows:

using ICSharpCode.TextEditor.Document;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace JackWangCUMT.WinForm{ /// /// The class to generate the foldings, it implements ICSharpCode.TextEditor.Document.IFoldingStrategy /// public class MingFolding : IFoldingStrategy { /// /// Generates the foldings for our document. /// /// The current document. /// The filename of the document. /// Extra parse information, not used in this sample. /// A list of FoldMarkers. public List GenerateFoldMarkers(IDocument document, string fileName, object parseInformation) { List list = new List(); //stack First-in, first-out var startLines = new Stack(); // Create foldmarkers for the whole document, enumerate through every line. for (int i = 0; i

< document.TotalNumberOfLines; i++) { // Get the text of current line. string text = document.GetText(document.GetLineSegment(i)); if (text.Trim().StartsWith("#region")) // Look for method starts { startLines.Push(i); } if (text.Trim().StartsWith("#endregion")) // Look for method endings { int start = startLines.Pop(); // Add a new FoldMarker to the list. // document = the current document // start = the start line for the FoldMarker // document.GetLineSegment(start).Length = the ending of the current line = the start column of our foldmarker. // i = The current line = end line of the FoldMarker. // 7 = The end column list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.Region, "...")); } //支持嵌套 {} if (text.Trim().StartsWith("{")) // Look for method starts { startLines.Push(i); } if (text.Trim().StartsWith("}")) // Look for method endings { if (startLines.Count >

0) { int start = startLines.Pop(); list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.TypeBody, "...} ")); } } // /// if (text.Trim().StartsWith("/// ")) // Look for method starts { startLines.Push(i); } if (text.Trim().StartsWith("/// ")) // Look for method endings { int start = startLines.Pop(); //Get comment text (including spaces) string display = document.GetText(document.GetLineSegment(start + 1).Offset, document.GetLineSegment(start + 1).Length); //remove /// display = display.Trim().TrimStart('/'); list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.TypeBody, display)); } } return list; } }}

III. Highlight configuration

Copy CSharp-Mode.xshd to JackCSharp-Mode.xshd, modify the name to: SyntaxDefinition name = "JackC#", and add highlighted keywords, as follows:

The JackWang that appears in the code will be highlighted. The following code snippet loads the custom highlight file and uses SetHighlighting to set it up. Note that there must be a xshd configuration file in the directory, otherwise the highlight will be invalid.

textEditor.Encoding = System.Text.Encoding.UTF8; textEditor.Font = new Font ("Hack",12); textEditor.Document.FoldingManager.FoldingStrategy = new JackWangCUMT.WinForm.MingFolding(); textEditor.Text = sampleCode; //Custom Code Highlight string path = Application.StartupPath+ "\\HighLighting"; FileSyntaxModeProvider fsmp; if (Directory.Exists(path)) { fsmp = new FileSyntaxModeProvider(path); HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmp); textEditor.SetHighlighting("JackC#"); }

To keep the code folded properly, listen for text changes like this:

private void TextEditor_TextChanged(object sender, EventArgs e) { //Update for code folding textEditor.Document.FoldingManager.UpdateFoldings(null, null); }

Finally, we can define a code formatting class to format C#code:

Thank you for reading this article carefully. I hope Xiaobian can share the ". NET code editing control ICSharpCode.TextEditor how to use" this article is helpful to everyone. At the same time, I hope you will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report