gpt4 book ai didi

C# 复杂的 TreeView 设计

转载 作者:太空狗 更新时间:2023-10-29 21:29:04 25 4
gpt4 key购买 nike

我正在尝试设计一个带有 richtextbox 控件的类 IDE(不可编辑)程序。基本上,我需要位于 RTB 左侧的 TreeView ,以便在用户单击 +/- 按钮时展开/折叠代码的特定部分。可展开可折叠范围定义为大括号所在的位置。例如在 RTB 中,如果我有类似的东西:

int main()
{
if (...)
{
if (...)
{
}
}
else
{
}
}

如果我点击最上面的大括号,它会折叠 main 函数中的所有内容。基本上,大括号内包含的就是折叠的内容。所以总而言之,我正在尝试设计一些与 Visual Studio 的展开/折叠代码功能非常相似的东西,除了它还使用 if/else 函数执行此操作。

我知道括号匹配算法,我实现了一个堆栈来了解哪对括号匹配(行号存储在元组列表中)。

我主要遇到的问题是如何着手设计实际的 TreeView 。我需要 TreeView 以线性方式显示,其中没有节点添加到另一个节点之上。我不知道有什么方法可以添加展开/折叠按钮而无需在另一个节点上实际添加子节点。

此外,除了 +/- 按钮和一条垂直线外,我需要 TreeView 节点不可编辑、不可见且不可点击。

最后,假设我满足上述要求,我还需要 RTB 的垂直滚动事件来正确滚动 TreeView 。也就是说,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++;
}
}

这是 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;
}
}

对于解决此问题的任何指导,我将不胜感激。提前致谢。

最佳答案

您应该看看 Scintilla 和 .NET 版本这里:http://scintillanet.codeplex.com/ .

它包含用于解决此类问题的源代码,但老实说,我只是使用该控件并将其设为只读来解决您的编程要求。

关于C# 复杂的 TreeView 设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11112170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com