【问题标题】:Making a text editor with multiple pages using richtextboxes使用richtextboxes制作具有多页的文本编辑器
【发布时间】:2017-01-26 14:50:06
【问题描述】:

我正在尝试为我的文本编辑器提供多页模式问题是当 Richtextbox 到达它调整大小的最后一行并添加一个不是我想要的滚动条时,我编写了一个代码来传输richtextbox 的最后一行到后面的那个,但它正在移动整个文本,而且有点迟钝,任何帮助将不胜感激

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

【问题讨论】:

  • Way 一个 SO 问题的代码太多了,IMO。
  • 对不起这里的菜鸟
  • 我认为问题在于使用计时器来移动行,但是我应该使用其他什么线程 idk 可能已经卡在这里好几天了......

标签: c# winforms richtextbox rich-text-editor


【解决方案1】:

RichTextBox 对这类事情很痛苦,因为要改变一小部分文本,您必须首先实际选择文本(看起来您正在尝试这样做)并确保更改仅影响该文本.这在内存使用方面有点令人讨厌,但是通过确定每页需要多少个字符并订阅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 放在自定义“页面”控件中。如果你没有,我希望我的代码能告诉你为什么你可能想要。

【讨论】:

  • 我无法预测用户可能使用多种字体和大小以及跳线等的文本限制。
  • 这与上面的代码无关,认为您可能还想在 Environment.NewLine 上 Split() 处理跳过的行。然而,存储字体是一个非常不同的故事。那是一种完全不同类型的信息。可以做到,但您最好使用支持类似书本分页的第三方控件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多