详细介绍一款.NET代码编辑控件(ICSharpCode.TextEditor)


本文摘自PHP中文网,作者Y2J,侵删。

这篇文章主要给大家介绍了.NET中用ICSharpCode.TextEditor自定义代码折叠与高亮的相关资料,文中通过示例代码与图片介绍的很详细,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

ICSharpCode.TextEditor 是一款非常不错的.NET代码编辑控件,内置了多种高亮语言支持,同时完美支持中文,非常赞!

先来看一下运行效果:

一、项目结构

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

二、代码折叠

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

using ICSharpCode.TextEditor.Document;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace JackWangCUMT.WinForm

{

  

 /// <summary>

 /// The class to generate the foldings, it implements ICSharpCode.TextEditor.Document.IFoldingStrategy

 /// </summary>

 public class MingFolding : IFoldingStrategy

 {

  /// <summary>

  /// Generates the foldings for our document.

  /// </summary>

  /// <param name="document">The current document.</param>

  /// <param name="fileName">The filename of the document.</param>

  /// <param name="parseInformation">Extra parse information, not used in this sample.</param>

  /// <returns>A list of FoldMarkers.</returns>

  public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)

  {

   List<FoldMarker> list = new List<FoldMarker>();

   //stack 先进先出

   var startLines = new Stack<int>();

   // 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, "...}"));

     }

    }

 

 

    // /// <summary>

    if (text.Trim().StartsWith("/// <summary>")) // Look for method starts

    {

     startLines.Push(i);

    }

    if (text.Trim().StartsWith("/// <returns>")) // 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;

  }

 }

}

三、高亮配置

阅读剩余部分

相关阅读 >>

分享asp.net学习笔记(13)razor 语法详解

[asp.net mvc 小牛之路]07 - url routing

使用asp.net中mvc引擎开发插件系统的示例详解

分享asp.net学习笔记(11)webpages php

asp.net中healthmonitor属性的用法教程

mvc页面重定向的asp代码讲解

如何使用会话状态(asp.net web 服务)

asp.net(c#)如何读取excel的文件的实例详解

分享asp.net core在开发环境中保存机密(user secrets)的实例

图文详解asp.net百度ueditor编辑器实现上传图片添加水印效果实例

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...