I am trying to create an IDE-like (non-editable) program with a richtextbox control. Basically, I need a tree, which is located to the left of RTB, to expand / collapse part of my code whenever a user presses the +/- buttons. Expandable expandable ranges are defined as wherever braces are visible. For example, in RTB, if I had something like:
int main() { if (...) { if (...) { } } else { } }
If I clicked on the topmost brace, it would destroy everything inside the main function. Basically, what is contained within this curly brace is what develops. So, in general, I'm trying to create something that is very similar to the Visual Studio expand / collapse function, except that it also works with if / else functions.
I know the parenthesis matching algorithm, and I implemented a stack to find out which pairs of parentheses match (line numbers are stored in the tuple list).
The problem I am facing is how to get started developing the actual tree. I need the tree structure to be linear, where no node is added on top of the other. I don't know of any approach that can add the expand / collapse button without actually adding child nodes on top of another node.
In addition, with the exception of the +/- buttons and the only vertical line, I need the tree nodes to be inaccessible for editing, invisible and not clickable.
Finally, and this assumes that if I have fulfilled the above requirements, I need the RTB vertical scroll event to properly scroll through the tree structure. That is, the Treeview collapse / expand section will be updated based on the part of the code visible in RTB.
Here is the section of code that I use to initialize the tree:
public partial class LogicSimulationViewerForm : Form { private List<Tuple<string,Boolean>> visibleLines = new List<Tuple<string,Boolean>>(); private List<Tuple<int, int>> collapseRange = new List<Tuple<int, int>>(); private void TreeInit() { TreeNode tn; Stack<int> openBracketLine = new Stack<int>(); int i = 0; TreeLogicCode.Nodes.Clear(); foreach (string s in rtbLogicCode.Lines) { visibleLines.Add(Tuple.Create(s, true)); if (s == "{") { openBracketLine.Push(i); } else if (s == "}") { collapseRange.Add(Tuple.Create(openBracketLine.Pop(),i)); } i++; } }
Here is the source code for Designer.sc, although I believe that this is not really necessary, but just in case:
namespace DDCUI { partial class LogicSimulationViewerForm {
I would really appreciate any advice on resolving this issue. Thanks in advance.