l46*_*kok 6 .net c# treeview winforms
我正在尝试使用richtextbox控件设计类似IDE(不可编辑)的程序.基本上,我需要位于RTB左侧的树视图,以便在用户单击+/-按钮时展开/折叠我的代码的某一部分.可扩展的可折叠范围定义为可以看到大括号的位置.例如在RTB中,如果我有类似的东西:
int main()
{
if (...)
{
if (...)
{
}
}
else
{
}
}
Run Code Online (Sandbox Code Playgroud)
如果我点击最顶部的花括号,它会折叠主函数内的所有内容.基本上,大括号里面包含的是折叠的东西.总而言之,我正在尝试设计一些与Visual Studio的展开/折叠代码函数非常相似的东西,除了它也使用if/else函数.
我知道括号匹配算法,我实现了一个堆栈,以了解哪些括号对匹配(行号存储在元组列表中).
我主要关注的问题是如何设计实际的树视图.我需要树视图以线性方式,其中没有节点添加到另一个上面.我不知道有任何方法可以添加展开/折叠按钮,而无需在另一个节点上实际添加子节点.
此外,除了+/-按钮和单个垂直线之外,我需要树视图节点不可编辑,不可见且不可点击.
最后,假设我满足上述要求,我需要RTB的垂直滚动事件来正确滚动树视图.也就是说,Treeview的折叠/展开部分将根据RTB上可见的代码部分进行更新.
这是我用来初始化树的代码的一部分:
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++;
}
}
Run Code Online (Sandbox Code Playgroud)
这是Designer.sc源代码,虽然我认为这不是必需的,但以防万一:
namespace DDCUI
{
partial class LogicSimulationViewerForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.TreeLogicCode = new System.Windows.Forms.TreeView();
this.labelLogicCode = new System.Windows.Forms.Label();
this.rtbLogicCode = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// TreeLogicCode
//
this.TreeLogicCode.Dock = System.Windows.Forms.DockStyle.Left;
this.TreeLogicCode.Location = new System.Drawing.Point(50, 0);
this.TreeLogicCode.Name = "TreeLogicCode";
this.TreeLogicCode.Scrollable = false;
this.TreeLogicCode.Size = new System.Drawing.Size(40, 600);
this.TreeLogicCode.TabIndex = 4;
//
// labelLogicCode
//
this.labelLogicCode.BackColor = System.Drawing.Color.LightGray;
this.labelLogicCode.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.labelLogicCode.Dock = System.Windows.Forms.DockStyle.Left;
this.labelLogicCode.ForeColor = System.Drawing.SystemColors.ControlText;
this.labelLogicCode.Location = new System.Drawing.Point(0, 0);
this.labelLogicCode.Margin = new System.Windows.Forms.Padding(3);
this.labelLogicCode.Name = "labelLogicCode";
this.labelLogicCode.Padding = new System.Windows.Forms.Padding(3);
this.labelLogicCode.Size = new System.Drawing.Size(50, 600);
this.labelLogicCode.TabIndex = 3;
this.labelLogicCode.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// rtbLogicCode
//
this.rtbLogicCode.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtbLogicCode.Location = new System.Drawing.Point(90, 0);
this.rtbLogicCode.Name = "rtbLogicCode";
this.rtbLogicCode.Size = new System.Drawing.Size(510, 600);
this.rtbLogicCode.TabIndex = 5;
this.rtbLogicCode.Text = "";
this.rtbLogicCode.VScroll += new System.EventHandler(this.rtbLogicCode_VScroll);
//
// LogicSimulationViewerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(600, 600);
this.Controls.Add(this.rtbLogicCode);
this.Controls.Add(this.TreeLogicCode);
this.Controls.Add(this.labelLogicCode);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "LogicSimulationViewerForm";
this.Text = "LogicSimulationViewerForm";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TreeView TreeLogicCode;
private System.Windows.Forms.Label labelLogicCode;
private System.Windows.Forms.RichTextBox rtbLogicCode;
}
}
Run Code Online (Sandbox Code Playgroud)
我真的很感激任何解决这个问题的指导.提前致谢.
您应该在这里查看Scintilla和.NET版本:http://scintillanet.codeplex.com/.
它包含解决这些问题的源代码,但老实说,我只是使用控件并将其设置为只读以解决您的编程需求.
| 归档时间: |
|
| 查看次数: |
1344 次 |
| 最近记录: |