【问题标题】:C# Intricate Treeview DesignC# 复杂的树视图设计
【发布时间】:2012-06-20 02:48:45
【问题描述】:

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

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

如果我点击最上面的大括号,它会折叠主函数内的所有内容。基本上,大括号内包含的内容就是折叠的内容。所以总而言之,我正在尝试设计一些与 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++;
        }
    }

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

我非常感谢任何有关解决此问题的指导。提前致谢。

【问题讨论】:

    标签: c# .net winforms treeview


    【解决方案1】:

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

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

    【讨论】:

    • 不幸的是,上面的问题描述正是客户想要的程序。我尝试打开 Scantilla,但它无法在我的机器上编译,这就是我来这里寻求帮助的原因。
    • 你不需要编译它,只需使用 DLL 将它添加到 Toolbox 并在 Form 上拖动即可。
    • 实际上我正在开发的程序是用于商业目的,由于许可,我似乎无法使用提到的开源项目。
    • 我不知道,这似乎不是很严格:特此授予以任何目的免费使用、复制、修改和分发此软件及其文档的许可,前提是上述版权声明出现在所有副本中,并且该版权声明和本许可声明都出现在支持文档中。把它放在你的帮助信息框中:)
    • @SokwhanHuh,Kell 是正确的,许可证意味着您无需付费即可使用它。 MMK 也是正确的,只需下载二进制文件并引用它们,无需编译。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多