在Richtextbox中添加/启用VScroll,并在运行时动态加载内容

时间:2015-07-09 16:16:20

标签: c# winforms richtextbox vertical-scrolling

我正在尝试构建一个简单的UI,它分为左右两个面板。左侧是在richtextbox(RTB)内部构建的树视图,其中包含可单击的链接。在右边它是一个WebBrowser。 RTB的内容在运行时动态加载。它可能超过RTB的高度,因此需要垂直滚动。

我已经设置了RTB的一些关键属性(如下所示)但是未能看到VScroll已启用,即使其中的内容超出了它的显示范围。

所以这里有一些代码片段:             private void InitializeComponent()         {             .........

        this.splitContainer1 = new System.Windows.Forms.SplitContainer();
        this.SidePane = new System.Windows.Forms.RichTextBox();
        this.Browser = new System.Windows.Forms.WebBrowser();
        this.splitContainer1.Panel1.SuspendLayout();
        this.splitContainer1.Panel2.SuspendLayout();
        this.splitContainer1.SuspendLayout();
        this.SuspendLayout();

        // splitContainer1
        // 
        this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.splitContainer1.Location = new System.Drawing.Point(0, 25);
        this.splitContainer1.Name = "splitContainer1";
        // 
        // splitContainer1.Panel1
        // 
        this.splitContainer1.Panel1.AutoScroll = true;
        this.splitContainer1.Panel1.Controls.Add(this.SidePane);
        // 
        // splitContainer1.Panel2
        // 
        this.splitContainer1.Panel2.AutoScroll = true;
        this.splitContainer1.Panel2.Controls.Add(this.Browser);
        this.splitContainer1.Size = new System.Drawing.Size(1110, 501);
        this.splitContainer1.SplitterDistance = 127;
        this.splitContainer1.TabIndex = 5;
        // 
        // SidePane
        // 
        this.SidePane.BorderStyle = System.Windows.Forms.BorderStyle.None;
        this.SidePane.Cursor = System.Windows.Forms.Cursors.Arrow;
        this.SidePane.Dock = System.Windows.Forms.DockStyle.Top;
        this.SidePane.EnableAutoDragDrop = true;
        this.SidePane.Location = new System.Drawing.Point(0, 0);
        this.SidePane.Name = "SidePane";
        this.SidePane.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
        this.SidePane.Size = new System.Drawing.Size(127, 481);
        this.SidePane.TabIndex = 0;
        this.SidePane.Text = "";
        this.SidePane.WordWrap = false;

        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoScroll = true;
        this.ClientSize = new System.Drawing.Size(1110, 548);
        this.Controls.Add(this.progressBar);
        this.Controls.Add(this.address);
        this.Controls.Add(this.splitContainer1);
        this.Controls.Add(this.statusbar);
        this.Controls.Add(this.GoButton);
        this.Controls.Add(this.ForwardButton);
        this.Controls.Add(this.BackButton);
        this.Controls.Add(this.toolStrip1);
        this.Name = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.statusbar.ResumeLayout(false);
        this.statusbar.PerformLayout();
        this.splitContainer1.Panel1.ResumeLayout(false);
        this.splitContainer1.Panel2.ResumeLayout(false);
        ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
        this.splitContainer1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private void loadInit(string file)
    {
        // load data from a file
        ............

        foreach (String node in nodes)
        {

            Hashtable hash = (Hashtable) parser.GetInitPairs(node);

            // create nodes of a tree view as lables and add them to the RTB
            Label lbl = new Label();
            lbl.Text = "[" + node + "]";
            lbl.Font = new Font("Arial", size);
            int x = X1, y = Y1;
            Point p = new Point(x, y);
            lbl.Location = p;
            this.SidePane.Controls.Add(lbl);
            Y1 += incr;

            // grab the links as sub-nodes and add them to the RTB
            foreach(String key in hash.Keys)
            {
                String val = (String) hash[key];
                Label klbl = new Label();
                klbl.Text = key;
                int X = X2, Y = Y1;
                Point kp = new Point(X, Y);
                klbl.Location = kp;
                klbl.ForeColor = Color.Blue;
                klbl.Font = new Font("Arial", size);

                // Clickable event handlers
                klbl.DoubleClick += (s, e) => { Browser.Navigate(/*node == "Poll"*/ val.Contains("{0}") && symbol != null ? String.Format(val, symbol) : val); klbl.ForeColor = Color.CadetBlue; };

                this.SidePane.Controls.Add(klbl);               

                Y1 += incr;
            }

        }

    }

我很感激您是否可以帮助解决这些问题:1)当内容多于显示内容时启用VScroll; 2)如果用户向上或向下滚动RTB区域,请处理事件处理。

0 个答案:

没有答案