C # WinForm Adding line numbers to RichTextBox tips

Source: Internet
Author: User
Tags add numbers

When the code is displayed, there is a very convenient and comfortable line number (probably a personal habit).

This reminds me of the blog Garden Code, add line number actually is in each line of code to add numbers, can only say is the next, as a Virgo I was very dissatisfied with this, although once also thought about it.

Test:

1 usingSystem;2 usingSystem.IO;3 usingSystem.Collections.Generic;4 usingSystem.ComponentModel;5 usingSystem.Data;6 usingSystem.Drawing;7 usingSystem.Linq;8 usingSystem.Text;9 usingSystem.Threading.Tasks;Ten usingSystem.Windows.Forms;

Copy and paste:

1 using System; 2 using System.IO;  3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Data; 6 using system.dr awing; 7 using System.Linq; 8 using System.Text; 9 using system.threading.tasks;10 using System.Windows.Forms;

  Fortunately, the blog Park provides code control with the ability to copy code:

I am in the spirit of not blowing not black attitude, we do not hit me "cover the face."
  VS inside for the text display, the RichTextBox control does not show the line number function, so worry about the children's shoes can take the following methods:
Interface design:

Control: Panel,richtextbox

The following functions are generated in the RichTextBox:

Call the following code in the function to draw the line number in the panel:

        Private voidShowlineno () {//get current coordinate informationPoint P = This. richtextbox1.location; intCrntfirstindex = This. Richtextbox1.getcharindexfromposition (P); intCrntfirstline = This. Richtextbox1.getlinefromcharindex (Crntfirstindex); Point Crntfirstpos= This. Richtextbox1.getpositionfromcharindex (Crntfirstindex); P.Y+= This. Richtextbox1.height; intCrntlastindex = This. Richtextbox1.getcharindexfromposition (P); intCrntlastline = This. Richtextbox1.getlinefromcharindex (Crntlastindex); Point Crntlastpos= This. Richtextbox1.getpositionfromcharindex (Crntlastindex); //Preparing for drawingGraphics g = This. Panel1.            CreateGraphics (); Font Font=NewFont ( This. Richtextbox1.font, This. RichTextBox1.Font.Style); SolidBrush Brush=NewSolidBrush (Color.green); //Paint Start//Refresh CanvasRectangle rect= This. Panel1.            ClientRectangle; Brush. Color= This. Panel1.            BackColor; G.fillrectangle (Brush,0,0, This. Panel1. Clientrectangle.width, This. Panel1.            Clientrectangle.height); Brush. Color= Color.White;//Reset Brush Color//Draw line number            intLinespace =0; if(Crntfirstline! =crntlastline) {Linespace= (CRNTLASTPOS.Y-CRNTFIRSTPOS.Y)/(Crntlastline-crntfirstline); }            Else{linespace= Convert.ToInt32 ( This. richTextBox1.Font.Size); }            intBrushx = This. Panel1. Clientrectangle.width-convert.toint32 (font. Size *3); intBrushy = crntlastpos.y + convert.toint32 (font. Size *0.21f);//amazing algorithm Ah!!              for(inti = Crntlastline; I >= crntfirstline; i--) {g.drawstring ((i+1).                ToString (), font, brush, brushx, brushy); Brushy-=Linespace;            } g.dispose (); Font.            Dispose (); Brush.        Dispose (); }

Call the function in the previously generated function code:

        Private void richtextbox1_textchanged (object  sender, EventArgs e)        {            Showlineno ();        }         Private void richtextbox1_vscroll (object  sender, EventArgs e)        {            Showlineno ();        }

Show:

Although the effect is not as good as expected, but simple, MARK.

Why do you bring a picture at the end of each essay? I know no picture is not attractive to see haha.

To be clear, images are mostly "shy" from the internet.

C # WinForm adding line numbers to RichTextBox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.