资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

.NET代码编辑控件ICSharpCode.TextEditor怎么用-创新互联

这篇文章主要介绍了.NET代码编辑控件ICSharpCode.TextEditor怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

站在用户的角度思考问题,与客户深入沟通,找到杞县网站设计与杞县网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、成都网站设计、企业官网、英文网站、手机端网站、网站推广、域名注册网站空间、企业邮箱。业务覆盖杞县地区。

先来看一下运行效果:

.NET代码编辑控件ICSharpCode.TextEditor怎么用

一、项目结构

.NET代码编辑控件ICSharpCode.TextEditor怎么用

这里需要注意lib文件夹下导入的类库,这个Demo需要这些dll.

二、代码折叠

需要实现IFoldingStrategy中的 GenerateFoldMarkers 方法,代码如下:

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 先进先出
   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();
     //获取注释文本(包括空格)
     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;
  }
 }
}

三、高亮配置

拷贝CSharp-Mode.xshd为 JackCSharp-Mode.xshd ,将其中的名字修改为: SyntaxDefinition name = "JackC#" ,并添加高亮关键字,如下:

.NET代码编辑控件ICSharpCode.TextEditor怎么用

这样代码中出现的JackWang就会高亮。下面的代码片段将自定义高亮文件进行加载,并用SetHighlighting进行设置,这里一定注意目录下必须有xshd的配置文件,否则高亮将失效。

textEditor.Encoding = System.Text.Encoding.UTF8;
 textEditor.Font = new Font("Hack",12);
 textEditor.Document.FoldingManager.FoldingStrategy = new JackWangCUMT.WinForm.MingFolding();
 textEditor.Text = sampleCode;

 //自定义代码高亮
 string path = Application.StartupPath+ "\\HighLighting";
 FileSyntaxModeProvider fsmp;
 if (Directory.Exists(path))
 {
  fsmp = new FileSyntaxModeProvider(path);
  HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmp);
  textEditor.SetHighlighting("JackC#");
 }

为了保持代码适时进行折叠,这里监听文本变化,如下所示:

   private void TextEditor_TextChanged(object sender, EventArgs e)
   {
    //更新,以便进行代码折叠
    textEditor.Document.FoldingManager.UpdateFoldings(null, null);
   }

最后说明的是,我们可以定义一个格式化代码的类,来格式化C#代码:

.NET代码编辑控件ICSharpCode.TextEditor怎么用

.NET代码编辑控件ICSharpCode.TextEditor怎么用

感谢你能够认真阅读完这篇文章,希望小编分享的“.NET代码编辑控件ICSharpCode.TextEditor怎么用”这篇文章对大家有帮助,同时也希望大家多多支持创新互联成都网站设计公司,关注创新互联成都网站设计公司行业资讯频道,更多相关知识等着你来学习!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:.NET代码编辑控件ICSharpCode.TextEditor怎么用-创新互联
标题路径:http://cdkjz.cn/article/gjhhs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220