使用richtextboxes使多个页面的文本编辑器

问题描述:

我想给我的文本编辑器多页面模式问题是当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

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