Reputation: 23
So I am working on an program that offers the user a 3-D visualization of data structures and sort algorithms. 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. 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. I am then writing the text to the 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;" then draw the 3d cube (already done), then highlight the "stackTop++" line. 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.
//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++;
}
Upvotes: 1
Views: 10403
Reputation: 117230
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:
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;
}
Upvotes: 7