c# 代码编辑控件(代码着色控件) ICSharpCode.TextEditor 简单应用示例

ICSharpCode.TextEditor 控件源自开源项目 SharpDevelop,是一款非常不错的.NET代码编辑控件,内置了多种高亮语言支持,对中文支持友好。

为了方便使用,我在其控件上增加语言属性,可以直接在属性栏设置语言,同时示例中加入代码右键菜单功能,包括剪切、复制、粘贴、删除、全选。

右键菜单

 

属性栏直接选择语言

 

代码参考

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.TextEditor;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = textEditorControl1.ActiveTextAreaControl.SelectionManager.SelectedText;
            if (str == "") return;
            Clipboard.SetText(str);
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = Clipboard.GetText();
            textEditorControl1.ActiveTextAreaControl.TextArea.InsertString(str);

            if (textEditorControl1.ActiveTextAreaControl.SelectionManager.HasSomethingSelected)
                textEditorControl1.ActiveTextAreaControl.SelectionManager.RemoveSelectedText();
        }

        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = textEditorControl1.ActiveTextAreaControl.SelectionManager.SelectedText;
            if (str == "") return;
            Clipboard.SetText(str);
            textEditorControl1.ActiveTextAreaControl.SelectionManager.RemoveSelectedText();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = textEditorControl1.ActiveTextAreaControl.SelectionManager.SelectedText;
            if (str != "") textEditorControl1.ActiveTextAreaControl.SelectionManager.RemoveSelectedText();
        }

        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textEditorControl1.ActiveTextAreaControl.SelectionManager.SetSelection(
                textEditorControl1.Document.OffsetToPosition(0),
                textEditorControl1.Document.OffsetToPosition(textEditorControl1.Document.TextLength));
        }
    }
}

示例代码下载

https://download.csdn.net/download/fxziyu/10883752

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值