2017-01-26 30 views
-1

我想給我的文本編輯器多頁面模式問題是當richtextbox達到最後一行它調整大小,並添加一個滾動條,這不是我想要的,我做了一個代碼RichTextBox的最後一行轉移到下面的一個,但它的移動的全部文本,而不是和它是一種緩慢的,任何幫助,將不勝感激使用richtextboxes使多個頁面的文本編輯器

public partial class Form1 : Form 
 
     { 
 
      protected static bool GetVisibleScrollbars(Control ctl) 
 
      { 
 
       int wndStyle = Win32.GetWindowLong(ctl.Handle, Win32.GWL_STYLE); 
 
       bool vsVisible = (wndStyle & Win32.WS_VSCROLL) != 0; 
 

 
       return vsVisible; 
 
      } 
 
      public Form1() 
 
      { 
 
       InitializeComponent(); 
 
      } 
 
      List<RichTextBox> pages=new List<RichTextBox>(); 
 
      int currentdocindex = 0; 
 
      
 
      public void AddPage() 
 
      { 
 

 
       RichTextBox B = new RichTextBox(); 
 
       B.Size = richTextBox1.Size; 
 
       panel1.Controls.Add(B); 
 
       B.Location = new Point(pages[pages.Count - 1].Location.X, pages[pages.Count - 1].Location.Y + richTextBox1.Height + 20); 
 
       pages.Add(B); 
 
       B.SelectionIndent = 20; 
 
       B.SelectionRightIndent = 20; 
 
       B.Enter += new EventHandler(richTextBox_Enter); 
 
       
 
    } 
 
      private void richTextBox_Enter(object sender, EventArgs e) 
 
      { 
 

 
       int i = 0; 
 
       foreach (RichTextBox box in pages) 
 
       { 
 
        if (box == (RichTextBox)sender) 
 
        { 
 
         currentdocindex=i; 
 
         break; 
 
        } 
 
        i++; 
 
       } 
 
       label1.Text = (currentdocindex + 1).ToString(); 
 
      } 
 

 
      private void Form1_Load(object sender, EventArgs e) 
 
      { 
 
       pages.Add(richTextBox1); 
 
       richTextBox1.SelectionIndent = 20; 
 
       richTextBox1.SelectionRightIndent = 20; 
 
       
 
      } 
 

 
      private void richTextBox1_Enter(object sender, EventArgs e) 
 
      { 
 
       int i = 0; 
 
       foreach (RichTextBox box in pages) 
 
       { 
 
        if(box==(RichTextBox)sender) 
 
        { 
 
         currentdocindex=i; 
 
         break; 
 
        } 
 
        i++; 
 
       } 
 
    } 
 

 
      bool added = false; 
 
      
 

 
      private void timer1_Tick(object sender, EventArgs e) 
 
      { 
 
       
 
        int correntPageIndex = currentdocindex; 
 
       if (GetVisibleScrollbars(pages[currentdocindex])) 
 
       { 
 
        if (!added) 
 
        { 
 
         AddPage(); 
 
         added = true; 
 
        } 
 
       } 
 
       else 
 
       { 
 
     
 
       added = false; 
 
        
 
        } 
 
       } 
 
       
 
       if(GetVisibleScrollbars(pages[correntPageIndex])) 
 
       { 
 

 
        string LastLineText = pages[correntPageIndex].Lines[pages[correntPageIndex].Lines.Count() - 1]; 
 
        int LastLineStartIndex = pages[correntPageIndex].Text.LastIndexOf(LastLineText); 
 
        pages[correntPageIndex].SelectionStart = LastLineStartIndex; 
 
        pages[correntPageIndex].SelectionLength = pages[correntPageIndex].Text.Length - 1; 
 
        LastLineText = pages[correntPageIndex].SelectedRtf; 
 
        pages[correntPageIndex].Text = pages[correntPageIndex].Text.Remove(LastLineStartIndex); 
 
        pages[correntPageIndex + 1].SelectionStart = 0; 
 
        pages[correntPageIndex+1].SelectedRtf = LastLineText; 
 
       } 
 
         } 
 
     } 
 
     public class Win32 
 
     { 
 
      // offset of window style value 
 
      public const int GWL_STYLE = -16; 
 

 
      // window style constants for scrollbars 
 
      public const int WS_VSCROLL = 0x00200000; 
 
      public const int WS_HSCROLL = 0x00100000; 
 

 
      [DllImport("user32.dll", SetLastError = true)] 
 
      public static extern int GetWindowLong(IntPtr hWnd, int nIndex); 
 
     }

+0

對於一個SO問題,IMO太多的代碼。 –

+0

對不起在這裏 – Untouchable

+0

我認爲問題是使用計時器來移動線條,但是idk還應該使用其他線程idk在這裏呆了幾天...... – Untouchable

回答

0

RichTextBox是一個痛苦這種事情,因爲要改變你h的一小部分文字大概首先實際選擇文本(看起來您正在嘗試執行)並確保更改僅影響該文本。這對內存使用有點討厭,但通過確定每頁需要多少個字符並訂閱KeyDown Event以確定何時移動到新頁面,可能會更好。嘗試適應這樣的事情,看看它是否更好。

public void MyKeyDownHandler(object sender, System.Windows.Forms.KeyEventArgs e) 
{ 
     if(this.CurrentPageControl.RTB.Text.Length >= MY_LIMITING_CONSTANT_I_SET) 
     { 
      MyPageUserControl mpuc = new MyPageUserControl();    
      mpuc.RTB.Text = this.CurrentPageControl.RTB.Text.Split(' ').Last(); 
      thePageCollectionIPresumeYouHave.Add(mpuc); 
      this.CurrentPageControl = thePageCollectionIPresumeYouHave.Last(); 
      mpuc.RTB.Focus(); 
     } 
} 

警告:我完全從內存中完成,沒有機會閱讀所有代碼(我不得不瀏覽),因爲我在工作。

另一個警告:我假設你把你的RichTextBoxes放在一個自定義的「頁面」控件中。如果你沒有,我希望我的代碼告訴你爲什麼你可能想要。

+0

我無法預測用戶的文本限制可能會使用多種字體和大小,以及跳過行等。 – Untouchable

+0

這應該與上面的代碼無關,可能您還想在Environment.NewLine上使用Split()來處理跳過的行。然而,存儲字體是一個非常不同的故事。這是一種完全不同類型的信息。它可以完成,但你可能會更好用支持書籍式分頁的第三方控件。 – CDove

相關問題