如何在richtextbox中为关键字着色? [英] How to color the keyword in richtextbox?

查看:94
本文介绍了如何在richtextbox中为关键字着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码:

Here is my code:

private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            int i = richTextBox1.Find("int");
            if (i >= 0)
            {
                richTextBox1.Select(i, "int".Length);
                richTextBox1.SelectionColor = Color.Red;

                richTextBox1.Focus();
                richTextBox1.SelectionStart = richTextBox1.Text.Length;

                richTextBox1.ForeColor = Color.Black;
            }
            else
            {
                return;
            }
        }





它给了我2个问题:

1。找到并绘制int关键字的红色后,它不会停止。当我继续键入任何键时,它仍然会绘制红色。我不知道为什么。



2.我的代码只在第1行工作。当我按Enter键并输入int时,颜色为黑色。



你能告诉我如何解决它吗?

谢谢!



It gives to me 2 problems:
1. After finding and drawing red color for "int" keyword, it doesn't stop. When i continue typing any key, it still draws red color. I don't know why.

2. My code is only working in line 1. When I press Enter and type "int", the color is black.

Can you tell me how to fix it?
Thanks!

推荐答案

在此处搜索文章 RichTextBox语法高亮显示 [ ^ ]。有很多关于如何做的例子。
Search the articles here for "RichTextBox syntax highlighting[^]". There's a ton of examples on how to do it.


我将发布一个代码示例以帮助您入门,但请注意,此代码无法处理很多问题,例如:



1.当您在RichTextBox的最后一行之前的一行中插入'int'时

2.当您键入时'int'在第一行的开头。



要真正做出任何类型的强大的单词着色功能,你需要跟踪单词你使用某种记录其位置的数据结构着色; 变得棘手因为他们的位置可能会随着用户添加新内容或删除旧内容,后退空间等而改变。



想想如果将int出现的前景颜色设置为红色会发生什么,然后用户编辑文本以便int明确地更改为in:,您应该设置in的前景色回到默认的文字颜色。除非你保持某种跟踪所有int实例的结构,否则你不能这样做!



所以,请接受Dave的建议并研究这些例子这里是CodeProject。



这个示例只是为了让您了解保存状态(当前选择开始和选择颜色),并在为单词着色后恢复它:
I'll post a code example to help get you started, but keep mind there are a lot of issues this code doesn't handle, like:

1. when you insert 'int' in a line before the last line of the RichTextBox
2. when you type 'int' at the start of the first line.

To really make any kind of robust word-coloring function, you are going to need to keep track of the words you have colored using some kind of data structure that records their location; that gets tricky because their location may change any time the user adds new content, or deletes old content, back-spaces, etc.

Think about what would happen if you set the forecolor of an occurrence of "int" to red, and then the user edited the text so that "int" was changed to "in:" clearly, you should be setting the forecolor of "in" back to your default text-color. You can't do that unless you maintain some kind of structure that keeps track of all instances of "int" !

So, do take Dave's advice and study the examples here on CodeProject.

This sample is meant just to give you the idea of saving state (current selection start and selection color), and restoring it after you color a word:
private int currentSelectionIndex;
private Color currentSelectionColor;
private string stringToFind = " int ";

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    int i = richTextBox1.Text.LastIndexOf(stringToFind);

    if (i >= 0)
    {
        currentSelectionIndex = richTextBox1.SelectionStart;
        currentSelectionColor = richTextBox1.SelectionColor;

        richTextBox1.Select(i, stringToFind.Length);
        richTextBox1.SelectionColor = Color.Red;

        richTextBox1.SelectionStart = currentSelectionIndex;
        richTextBox1.SelectionColor = currentSelectionColor;
    }
}


这篇关于如何在richtextbox中为关键字着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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