如何选择RichTextBox的文本,然后颜色呢? [英] How to select text from the RichTextBox and then color it?

查看:242
本文介绍了如何选择RichTextBox的文本,然后颜色呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的编辑器如记事本+ ,提供简单的功能..我需要在丰富的文本框区域上色的特定词。我该怎么办呢?

I want to create a simple editor like Notepad++ with simple functionality... I need to color a specific word in the rich text box area. How can I do that?

例如:当用户写这些字,我想给他们颜色以蓝色。这些词是:,而如果尝试

For example: when the user write these word, I want to color them to the blue color. These words are: for, while, if, try, etc.

我怎样才能让RichTextBox中选择一个特定的单词,然后将它们上色?
而且,如果我想要评论和颜色一切后 // ,如何在RichTextBox中做了什么?

How can I make the richtextbox to select a specific word and then color them? And, if I want to make a comment and color everything after the //, how is that done in the richtextbox?

我如何号码在文本框中行了,所以我现在就可以当我在我的编辑器的编码行数?

How do I number the line in the text box, so I can now the line number when I'm coding in my editor?

推荐答案

下面是一些code可以以达到你想要的功能基础上。

Here's some code you can build on in order to achieve the functionality you want.

private void ColourRrbText(RichTextBox rtb)
{
    Regex regExp = new Regex("\b(For|Next|If|Then)\b");

    foreach (Match match in regExp.Matches(rtb.Text))
    {
        rtb.Select(match.Index, match.Length);
        rtb.SelectionColor = Color.Blue;
    }
}

在$ C $的CProject文章的启用语法一个RichTextBox 突出展示了如何使用正则表达式在一个RichTextBox进行语法高亮显示。具体来说,看 SyntaxRichtTextBox.cs 的实施。

The CodeProject article Enabling syntax highlighting in a RichTextBox shows how to use RegEx in a RichTextBox to perform syntax highlighting. Specifically, look at the SyntaxRichtTextBox.cs for the implementation.

这篇关于如何选择RichTextBox的文本,然后颜色呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆