2013-07-30 18 views
0

我要滾動時間盒,當我滾動客艙起來。 (不一定反之亦然)滾動兩個富文本框在一起,我無法弄清楚

我發現下面的代碼這樣做:

/// Subclass RichTextBox to add the capability to bind scrolling for multiple RichTextBoxs. 
/// This is useful for 'parallel' RTBs that require synchronized scrolling. 
/// Taken from https://gist.github.com/593809 
/// Added WM_HSCROLL 
/// Added BindScroll() to form a two-way linkage between RichTextBoxes. 
/// Example usage showing how to bind 3 RichTextBoxes together: 
/// rtb1.BindScroll(rtb2); 
/// rtb2.BindScroll(rtb3); 
/// rtb3.BindScroll(rtb1); 

class RichTextBoxSynchronizedScroll : RichTextBox 
{ 

    private const int WM_VSCROLL = 0x115; 
    private const int WM_HSCROLL = 0x114; 

    private List<RichTextBoxSynchronizedScroll> peers = new List<RichTextBoxSynchronizedScroll>(); 

    /// <summary> 
    /// Establish a 2-way binding between RTBs for scrolling. 
    /// </summary> 
    /// <param name="arg">Another RTB</param> 
    public void BindScroll(RichTextBoxSynchronizedScroll arg) 
    { 
     if (peers.Contains(arg) || arg==this) { return; } 
     peers.Add(arg); 
     arg.BindScroll(this); 
    } 

    private void DirectWndProc(ref Message m) 
    { 
     base.WndProc(ref m); 
    } 

    protected override void WndProc(ref Message m) 
    { 
     if (m.Msg == WM_VSCROLL || m.Msg == WM_HSCROLL) 
     { 
      foreach (RichTextBoxSynchronizedScroll peer in this.peers) 
      { 
       Message peerMessage = Message.Create(peer.Handle, m.Msg, m.WParam, m.LParam); 
       peer.DirectWndProc(ref peerMessage); 
      } 
     } 

     base.WndProc(ref m); 
    } 
} 

不過,我一直在破壞我的頭在這超過2小時試圖讓不同的代碼工作,但我不能讓任何人的工作,我只是比較剛剛開始編程,我想不出有什麼用此代碼做。

我試圖把它作爲我的窗體的代碼一個額外的類,但我不能實際應用BindScroll()到任何文本框,因爲我無法對其引用或實例。

也許我可以,但我不知道如何。我試過只使用類內部的代碼,沒有它本身就是一個類,但是這導致了錯誤。

任何幫助,將不勝感激......

回答

1

測試你的代碼後,它似乎工作確定。你的問題可能是你不知道如何使用的代碼,你已經宣佈新richtextboxes作爲RichTextBoxSynchronizedScroll不作爲標準RichTextBox

//Here is the test 
public partial class Form1 : Form { 
    public Form1(){ 
    InitializeComponent(); 
    rb1.Size = new Size(200,100); 
    rb2.Size = rb1.Size; 
    rb2.Left = rb1.Right + 5; 
    rb1.Parent = rb2.Parent = this; 
    rtb1.BindScroll(rtb2);  
    //try populating some data for both the richtextboxes 
    for(int i = 0; i < 200; i++) 
     rtb1.Text += Guid.NewGuid() + "\r\n"; 
    rtb2.Text = rtb1; 
    //now try scrolling the rtb1 
    //I suggest you should add WM_MOUSEWHEEL = 0x20a into the if statement 
    //something like this: if (m.Msg == WM_VSCROLL || m.Msg == WM_HSCROLL || m.Msg == WM_MOUSEWHEEL) ... 
    } 
    RichTextBoxSynchronizedScroll rtb1 = new RichTextBoxSynchronizedScroll(); 
    RichTextBoxSynchronizedScroll rtb2 = new RichTextBoxSynchronizedScroll(); 
} 
//That's all 
+0

謝謝您的回答,我改變了代碼。但問題是,我真的不知道從哪裏把代碼在我的程序以及如何使用BindScroll()在我的RichTextBoxes ... –

+0

@RamzesM看到我的編輯 –

+0

喔...謝謝! –

0

我一直解決這整個3小時...我想要的東西很簡單。我考慮了滾動的各個方面。 你只需要創建VScrollBar 和這樣定義

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e) 
    { 
     int position = text1.GetFirstCharIndexFromLine(vScrollBar1.Value); 
     //position only 0.character on line 
     textBox1.Select(position, 0);//if position=vScrollBar1.Value you would actually scrolling char by char 
     textBox2.Select(position, 0); 
     textBox1.ScrollToCaret(); 
     textBox2.ScrollToCaret(); 
    } 

滾動事件但願我能找到這樣的未來。