C # Write richTextBox with row number

Source: Internet
Author: User

Add two controls: Panel/RichTextBox

 

View sourceprint? 01 // Method

02

03 private void showLineNo ()

04

05 {

06

07 // obtain the current coordinate information

08

09 Point p = this. richTextBox1.Location;

10 int crntFirstIndex = this. richTextBox1.GetCharIndexFromPosition (p );

11

12 int crntFirstLine = this. richTextBox1.GetLineFromCharIndex (crntFirstIndex );

13

14 Point crntFirstPos = this. richTextBox1.GetPositionFromCharIndex (crntFirstIndex );

15

16 //

17 p. Y + = this. richTextBox1.Height;

18

19 //

20

21 int crntLastIndex = this. richTextBox1.GetCharIndexFromPosition (p );

22

23 int crntLastLine = this. richTextBox1.GetLineFromCharIndex (crntLastIndex );

24 Point crntLastPos = this. richTextBox1.GetPositionFromCharIndex (crntLastIndex );

25

26 //

27

28 //

29

30 // prepare for drawing

31 Graphics g = this. panel1.CreateGraphics ();

32

33 Font font = new Font (this. richTextBox1.Font, this. richTextBox1.Font. Style );

34

35 SolidBrush brush = new SolidBrush (Color. Green );

36

37 //

38 //

39

40 // start drawing

41

42 // refresh the canvas

43

44 Rectangle rect = this. panel1.ClientRectangle;

45 brush. Color = this. panel1.BackColor;

46

47g. FillRectangle (brush, 0, 0, this. panel1.ClientRectangle. Width, this. panel1.ClientRectangle. Height );

48

49 brush. Color = Color. Green; // reset the paint Color

50

51 //

52 // draw the row number

53

54 int lineSpace = 0;

55

56 if (crntFirstLine! = CrntLastLine)

57

58 {

59 lineSpace = (crntLastPos. Y-crntFirstPos. Y)/(crntLastLine-crntFirstLine );

60

61}

62

63 else

64

65 {

66 lineSpace = Convert. ToInt32 (this. richTextBox1.Font. Size );

67

68}

69

70 int brushX = this. panel1.ClientRectangle. Width-Convert. ToInt32 (font. Size * 3 );

71

72 int brushY = crntLastPos. Y + Convert. ToInt32 (font. Size * 0.21f); // amazing algorithm !!

73 for (int I = crntLastLine; I> = crntFirstLine; I --)

74

75 {

76

77g. DrawString (I + 1). ToString (), font, brush, brushX, brushY );

78

79 brushY-= lineSpace;

80}

81

82g. Dispose ();

83

84 font. Dispose ();

85

86 brush. Dispose ();

87}


Related Article

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.