Home > Backend Development > C#.Net Tutorial > Detailed explanation of how to implement synchronous scrolling of two richtextbox control scroll bars in C#

Detailed explanation of how to implement synchronous scrolling of two richtextbox control scroll bars in C#

黄舟
Release: 2017-05-21 10:45:40
Original
2556 people have browsed it

This article mainly introduces to you the simple method of C#realizing two richtextboxControl scroll bars to scroll synchronously. The introduction in the article is very detailed and has certain reference learning for everyone. Value, friends in need come and 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 custom 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;
    }
Copy after login

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
      {

      }
    }
Copy after login

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

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 locate the line number of richtexbox1 obtained in richtextbox2, so that richtextbox2 scrolls with the richtexbox1

scroll bar;

in the VScroll# of richTextBox1 Add the following code to ##Event. Note, do I have one here? , represents a certain offset, which may cause the two richtextboxes to be out of sync due to layout reasons (such as control size, etc.). Generally, just write 0. If the gap is too large, adjust it by yourself. Integer

value.

     private void richTextBox1_VScroll(object sender, EventArgs e)
     {
      int crntLastLine= GetLineNoVscroll(richTextBox1, panel1)-?;
       TrunRowsId(crntLastLine, richTextBox2);
     }
Copy after login
Finally, the two scroll bars are scrolled synchronously

Summary

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template