C# 突出显示richtextbox中的一行文本

C# 突出显示richtextbox中的一行文本,c#,winforms,richtextbox,C#,Winforms,Richtextbox,因此,我正在开发一个程序,为用户提供数据结构和排序算法的三维可视化。我想做的是在UI上有一个richtextbox,显示正在执行的特定算法的代码。然后,我希望在执行代码的过程中突出显示每一行代码。我只是想从可视化堆栈开始,因为在我学习和完成这个项目时,它更容易处理。现在我有一个C++的推送和POP函数的文本文件,我把文本保存到一个列表中。然后我将文本写入richtextbox。所有这些都很有效,但我不知道如何高亮显示一行,然后高亮显示下一行。例如,当我单击“推送”时,我希望它高亮显示“list[

因此,我正在开发一个程序,为用户提供数据结构和排序算法的三维可视化。我想做的是在UI上有一个richtextbox,显示正在执行的特定算法的代码。然后,我希望在执行代码的过程中突出显示每一行代码。我只是想从可视化堆栈开始,因为在我学习和完成这个项目时,它更容易处理。现在我有一个C++的推送和POP函数的文本文件,我把文本保存到一个列表中。然后我将文本写入richtextbox。所有这些都很有效,但我不知道如何高亮显示一行,然后高亮显示下一行。例如,当我单击“推送”时,我希望它高亮显示“list[stackTop]=newItem”;然后绘制3d立方体(已完成),然后高亮显示“stackTop++”行。然后用户可以再做一次,或者做任何他们想做的事情

class CppFunctionsArray
    {
    List<string> ReadFunctions = new List<string>();
    int Position = 0;

    //Reads input from selected file and stores into ReadFunctions Array;
    public void ReadInput(string fileName)
    {

        using (StreamReader r = new StreamReader(fileName))
        {
            string line;
            while ((line = r.ReadLine()) != null)
            {
                ReadFunctions.Add(line);
            }
        }
    }

    //Writes lines to a RichTextBox.
    public void WriteToRichTextBox(RichTextBox rtb, int startIndex, int endIndex, int  lineNumber)
    {
        Position = 0;
        for (int i = startIndex; i < endIndex; i++)
        {
            rtb.AppendText(ReadFunctions[i]);
            rtb.AppendText(Environment.NewLine);
            rtb.Font = new Font("times new roman", 12, FontStyle.Bold);

            //Temporary
            if (lineNumber == Position)
                  rtb.SelectionBackColor = Color.Red;
            Position++;

        }
    }

如果您正在寻找一种扩展方法来清除某行中所有行的背景色,然后为特定行上色,那么以下内容就足够了:

    public static void HighlightLine(this RichTextBox richTextBox, int index, Color color)
    {
        richTextBox.SelectAll();
        richTextBox.SelectionBackColor = richTextBox.BackColor;
        var lines = richTextBox.Lines;
        if (index < 0 || index >= lines.Length)
            return;
        var start = richTextBox.GetFirstCharIndexFromLine(index);  // Get the 1st char index of the appended text
        var length = lines[index].Length;
        richTextBox.Select(start, length);                 // Select from there to the end
        richTextBox.SelectionBackColor = color;
    }
publicstaticvoidhighlightline(此RichTextBox RichTextBox,int index,Color)
{
richTextBox.SelectAll();
richTextBox.SelectionBackColor=richTextBox.BackColor;
var lines=richTextBox.lines;
如果(索引<0 | |索引>=行.长度)
回来
var start=richTextBox.GetFirstCharIndexFromLine(index);//获取附加文本的第一个字符索引
变量长度=行[索引]。长度;
richTextBox.Select(开始,长度);//从头到尾选择
richTextBox.SelectionBackColor=颜色;
}

如果您正在寻找一种扩展方法来清除a中所有行的背景色,然后为特定行上色,那么以下内容就足够了:

    public static void HighlightLine(this RichTextBox richTextBox, int index, Color color)
    {
        richTextBox.SelectAll();
        richTextBox.SelectionBackColor = richTextBox.BackColor;
        var lines = richTextBox.Lines;
        if (index < 0 || index >= lines.Length)
            return;
        var start = richTextBox.GetFirstCharIndexFromLine(index);  // Get the 1st char index of the appended text
        var length = lines[index].Length;
        richTextBox.Select(start, length);                 // Select from there to the end
        richTextBox.SelectionBackColor = color;
    }
publicstaticvoidhighlightline(此RichTextBox RichTextBox,int index,Color)
{
richTextBox.SelectAll();
richTextBox.SelectionBackColor=richTextBox.BackColor;
var lines=richTextBox.lines;
如果(索引<0 | |索引>=行.长度)
回来
var start=richTextBox.GetFirstCharIndexFromLine(index);//获取附加文本的第一个字符索引
变量长度=行[索引]。长度;
richTextBox.Select(开始,长度);//从头到尾选择
richTextBox.SelectionBackColor=颜色;
}

1)看起来您正在使用winforms。你能证实吗?2) 你的问题是什么?你的代码不起作用了吗?如果是的话,你能描述一下这个bug的症状吗?是的,我正在使用winforms。代码中没有bug。问题是,我有一个富文本框,其中包含从文件中写入的“代码”。当我单击一个按钮(仅一次)时,我需要它高亮显示此文本框中的特定行。然后是下一个,等等。例如,如果用户正在可视化排序算法,我希望此文本框中的代码在执行时高亮显示。我可以加载代码,但突出显示部分给我带来了问题。1)看起来您正在使用winforms。你能证实吗?2) 你的问题是什么?你的代码不起作用了吗?如果是的话,你能描述一下这个bug的症状吗?是的,我正在使用winforms。代码中没有bug。问题是,我有一个富文本框,其中包含从文件中写入的“代码”。当我单击一个按钮(仅一次)时,我需要它高亮显示此文本框中的特定行。然后是下一个,等等。例如,如果用户正在可视化排序算法,我希望此文本框中的代码在执行时高亮显示。我可以加载代码,但突出显示部分给我带来了问题。