Home  >  Article  >  Backend Development  >  C# How to implement a simple method of synchronous scrolling of two richtextbox control scroll bars

C# How to implement a simple method of synchronous scrolling of two richtextbox control scroll bars

黄舟
黄舟Original
2017-05-28 10:08:181751browse

This article mainly introduces you to a simple method in C# to realize synchronous scrolling of two richtextbox control scroll bars. The introduction in the article is very detailed and has certain reference and learning value for everyone. Friends who need it can take a look below. .

Preface

Sometimes we need to implement comparison articles, etc., and often put the text into two richtextbox controls. However, if we need Scroll and view simultaneously to achieve better viewing effects.

Of course, the traditional method of overloading controls or customizing controls can achieve the goal, but it is very troublesome for novices or people who want to use this control only once. So, next I will provide a simple and quick way to implement: richtextbox scroll bar synchronization function.

The method is as follows:

First, we create two richtextbox controls in the winform form

Two methods are introduced below. I often use

The first method is to get the line number in the richtextbox control where the current mouse is located


private int GetLineNoVscroll(RichTextBox rtb)
    {
      //获得当前坐标信息
      Point p = rtb.Location;
      int crntFirstIndex = rtb.GetCharIndexFromPosition(p);
      int crntFirstLine = rtb.GetLineFromCharIndex(crntFirstIndex);
      return crntFirstLine;
    }

The second method is to quickly go to a certain line in the richtextbox control


private void TrunRowsId(int iCodeRowsID, RichTextBox rtb)
    {
      try
      {
        rtb.SelectionStart = rtb.GetFirstCharIndexFromLine(iCodeRowsID);
        rtb.SelectionLength = 0;
        rtb.ScrollToCaret();
      }
      catch
      {

      }
    }

With these two methods, we can achieve scroll bar synchronization function.

The idea is as follows: First, when richtextbox1 scrolls, obtain the mouse corresponding line number of richtextbox1 through the GetLineNoVscroll method. Then

uses the TrunRowsId method to position the obtained line number of richtexbox1 into richtextbox2, so that richtextbox2 scrolls with the richtexbox1

scroll bar;

In the VScroll event of richTextBox1 Add the following code, note, I have a? , represents a certain offset. The two richtextboxes may be out of sync due to layout reasons (such as control size, etc.). Generally, just write 0. If the difference is too large, adjust the value by yourself.

     private void richTextBox1_VScroll(object sender, EventArgs e)
     {
      int crntLastLine= GetLineNoVscroll(richTextBox1, panel1)-?;
       TrunRowsId(crntLastLine, richTextBox2);
     }

Finally, synchronous scrolling of two scroll bars is achieved

Summary

The above is the detailed content of C# How to implement a simple method of synchronous scrolling of two richtextbox control scroll bars. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn