[英]Highlighting a line of text in a richtextbox
So I am working on an program that offers the user a 3-D visualization of data structures and sort algorithms. 因此,我正在开发一个程序,向用户提供数据结构和排序算法的3-D可视化。 What I would like to do is have a richtextbox on the UI that shows the code for the particular algorithm that is being performed.
我想做的是在UI上有一个RichTextBox,其中显示了正在执行的特定算法的代码。 And then I would like to have each particular line of the code to be highlighted as it is being executed.
然后,我想在执行代码时突出显示每一行代码。 I just wanted to start with visualizing a stack since it is easier to deal with as I learn and work through this project.
我只是想从可视化堆栈开始,因为在我学习和完成此项目时,它更易于处理。 Right now I have a text file of c++ push and pop functions and I am saving the text into a list.
现在,我有一个包含c ++ push和pop函数的文本文件,并将文本保存到列表中。 I am then writing the text to the richtextbox.
然后,我将文本写入Richtextbox。 All of this is working but I don't know how to highlight a line and then highlight the next line.
所有这些都有效,但是我不知道如何突出显示一行然后突出显示下一行。 For example when I click "push" I would like it to highlight "list[stackTop] = newItem;"
例如,当我单击“推”时,我希望它突出显示“ list [stackTop] = newItem;”。 then draw the 3d cube (already done), then highlight the "stackTop++" line.
然后绘制3d立方体(已完成),然后突出显示“ stackTop ++”行。 Then the user can do it again or whatever else they want.
然后,用户可以再次执行此操作,也可以执行其他任何操作。
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++;
}
}
These are not topics they are teaching me college. 这些不是他们在教我大学的话题。 I am just teaching myself here.
我只是在这里自学。 So if I am approaching this totally wrong, I am open to anything here.
因此,如果我要解决这个完全错误的问题,那么我对这里的一切都持开放态度。
Here is my event handler for "stackPush" button. 这是我的“ stackPush”按钮的事件处理程序。
//Adds cube on top of the previous.
private void StackPush_Click(object sender, EventArgs e)
{
CppFunctionsArray ArrayOfFunctions = new CppFunctionsArray();
CodeTextBox.Clear();
ArrayOfFunctions.ReadInput("StackFunctions.txt");
//The 4 represents the line Number to highlight. TODO FIX THIS.
ArrayOfFunctions.WriteToRichTextBox(CodeTextBox, 1, 12,4);
//Draws a new cube of 1 unit length.
cube = new Visual();
//Adds cube to list;
cubeList.Add(cube);
cube.y = position;
position++;
}
If you are looking for an extension method to clear the background color from all lines of a RichTextBox , then color a specific line, the following should suffice: 如果您正在寻找一种扩展方法来清除RichTextBox的所有行的背景颜色,然后为特定的行着色,则以下内容就足够了:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.