2012-06-20 34 views
6

Tôi đang cố thiết kế một chương trình giống như IDE (Không thể chỉnh sửa) với điều khiển richtextbox. Về cơ bản, tôi cần treeview được đặt ở phía bên trái của RTB để mở rộng/thu gọn một phần nhất định của mã của tôi bất cứ khi nào người dùng nhấp vào nút +/-. Phạm vi có thể mở rộng có thể thu gọn được định nghĩa là bất cứ nơi nào các dấu ngoặc nhọn được nhìn thấy. Ví dụ trong RTB nếu tôi có một cái gì đó như:C# Thiết kế Treeview phức tạp

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

Nếu tôi nhấp vào dấu ngoặc nhọn trên cùng, nó sẽ thu gọn mọi thứ bên trong chức năng chính. Về cơ bản những gì chứa bên trong dấu ngoặc nhọn đó là những gì được gấp lại. Vì vậy, tóm lại, tôi đang cố gắng thiết kế một cái gì đó rất giống với chức năng mở rộng/sụp đổ mã của Visual Studio ngoại trừ nó cũng làm điều đó với các hàm if/else.

Tôi biết thuật toán khớp với khung và tôi đã triển khai ngăn xếp để biết cặp nào khớp với nhau (Số dòng được lưu trữ trong danh sách bộ tuple).

Vấn đề tôi đang có chủ yếu là làm thế nào để đi về thiết kế treeview thực tế. Tôi cần treeview để được trong một thời trang tuyến tính, nơi không có nút được thêm vào đầu khác. Tôi không biết về bất kỳ cách tiếp cận nào có thể thêm nút mở rộng/thu gọn mà không thực sự thêm các nút con trên đầu một nút khác.

Ngoài ra, ngoại trừ các nút +/- và một đường thẳng đứng số ít, tôi cần các nút treeview không thể chỉnh sửa, không hiển thị và không thể nhấp.

Cuối cùng và điều này giả định nếu tôi đã đáp ứng các yêu cầu trên, tôi cần sự kiện cuộn dọc của RTB để cuộn chính xác các ảnh treeview. Tức là, phần thu gọn/mở rộng của Treeview sẽ cập nhật dựa trên phần mã hiển thị trên RTB.

Đây là một phần của mã Tôi đang sử dụng để khởi tạo cây:

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++; 
     } 
    } 

Đây là mã nguồn Designer.sc, mặc dù tôi tin rằng điều này sẽ không thực sự cần thiết nhưng chỉ trong trường hợp:

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

tôi thực sự sẽ đánh giá cao bất kỳ hướng dẫn về giải quyết vấn đề này. Cảm ơn trước.

Trả lời

7

Bạn nên xem Scintilla và phiên bản .NET tại đây: http://scintillanet.codeplex.com/.

Nó bao gồm mã nguồn để giải quyết các loại vấn đề này, mặc dù thành thật mà nói, tôi chỉ sử dụng điều khiển và làm cho nó chỉ đọc để giải quyết các yêu cầu lập trình của bạn.

+0

Thật không may, mô tả sự cố ở trên chính xác là những gì khách hàng muốn cho chương trình. Tôi đã thử mở Scantilla nhưng nó không biên dịch trên máy tính của tôi, vì vậy đó là lý do tại sao tôi đến đây để được giúp đỡ. – l46kok

+0

Bạn không cần phải biên dịch nó chỉ sử dụng DLL thêm nó vào Toolbox và chỉ cần kéo vào Form. – MMK

+0

Thực ra chương trình tôi đang phát triển là vì mục đích thương mại và có vẻ như tôi không thể sử dụng dự án mã nguồn mở được đề cập do giấy phép. – l46kok

Các vấn đề liên quan