Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
28 captures
26 Jan 2008 - 20 Jan 2022
NovDECOct
Previous capture29Next capture
201020112013
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20111229191338/http://www.codeproject.com:80/KB/edit/numberedtextbox.aspx
Click here to Skip to main content
8,370,136 members and growing!
EmailPassword Lost password?
Home
Search within:




Licence 
First Posted 3 Nov 2005
Views 125,859
Downloads 1,930
Bookmarked 87 times

Numbering lines of RichTextBox in .NET 2.0

ByPetr Minarik | 3 Nov 2005
The standard RichTextBox does not allow numbering of lines. This user control does.
 
See Also
Print Article
add
Add to your CodeProject bookmarks
Discuss
Discuss this article
44
  4.68 (30 votes)
4 votes, 13.3%
1

2
3 votes, 10.0%
3
6 votes, 20.0%
4
17 votes, 56.7%
5
4.68/5 - 30 votes
4 removed
μ 4.11, σa 2.47 [?]
Sponsored Links

Introduction

Numbering of lines in text editors is a well known feature. But the standardRichTextBox in .NET 2.0 does not support this feature. It is also hard to find a suitable solution on the Internet. Especially, a solution that does not directly use Win32 functions.

TheRichTextBox is not a standard control in Windows Forms. It does not use theOnPaint method and other similar functions as it should and it also hides some of the important properties required for proper customization. One way to overcome this is to use win32 API functions and override the WndProc functions. I don't like this way of doing it but I am forced to use it again and again. I consider it as a fault of .NET developers.

Fortunately, for numbering lines ofRichTextBox, there is a satisfactory solution that does not use pure Win32 API functions.

Implementation

We implement theRichTextBox with numbered lines as aUserControl. We will not override anything in theRichTextBox, we will only use its events. OurUserControl namedNumberedTextBoxUC consists ofSplitContainer,Label (numberLabel) andRichTextBox.Label is used for displaying the line number andRichTextBox for the text content, both are contained inSplitterContainer.

The content ofnumberLabel is updated in theRichTextBox's event handlers. These events are:

  • OnTextChanged
  • OnVScroll
  • OnSizeChanged
  • OnFontChanged

Problems

There are several problems with this implementation. The first one is scrolling. Unlike the VS source code editor orTextBox control,RichTextBox uses smooth scrolling, thus scrolling with the scrollbar scrolls the text in pixels, not in lines. You will notice that the first line is displayed in half. This is not always a wanted behaviour and I would appreciate the possibility to turn it off. Another problem is the redrawing speed of largeLabels, you cannot afford to print too many lines toLabel in eachOnTextChanged event handler. Another strange problem is theRichTextBox in .NET 2.0 uses strange line indentation, which is impossible to turn off or set to zero. The same font inLabel andRichTextBox results in different line positions when the controls are top aligned.

Solution

I am displaying only the numbers of visible lines, thus the unnecessary hidden line numbers are not printed. The update function is calledupdateNumberLabel(). It uses theRichTextBox functionsGetCharIndexFromPosition andGetLineFromCharIndex to determine the first and last visible line numbers.

privatevoid updateNumberLabel(){//we get index of first visible char and//number of first visible line    Point pos =new Point(0,0);int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);//now we get index of last visible char//and number of last visible line    pos.X = ClientRectangle.Width;    pos.Y = ClientRectangle.Height;int lastIndex = richTextBox1.GetCharIndexFromPosition(pos);int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);//this is point position of last visible char, we'll//use its Y value for calculating numberLabel size    pos = richTextBox1.GetPositionFromCharIndex(lastIndex);//finally, renumber label    numberLabel.Text ="";for (int i = firstLine; i <= lastLine +1; i++)    {        numberLabel.Text += i +1 +"\n";    }}

For different line indentations I have found a constant, which works best for font size 8. The size of theLabel font is bigger for this constant. I hope, in future versions of .NET this issue will be fixed and both the fonts will be exactly the same, as it was in .NET 1.1.

public NumberedTextBoxUC(){    InitializeComponent();    numberLabel.Font =new Font(richTextBox1.Font.FontFamily,                               richTextBox1.Font.Size +1.019f);}

Smooth scrolling is another issue which causes a lot of troubles. I use the smallnumberLabel location update in eachOnVScroll event handler. TheLabel is moved about as many pixels different from multiples of theRichTextBox font height, thus the modulo text position of the font height. The reverse solution, to update the text position to be line aligned, is in my opinion impossible without using Win32 functions. Updating the text position withRichTextBox functions in this way results in text shivering.

privatevoid richTextBox1_VScroll(object sender, EventArgs e){//move location of numberLabel for amount//of pixels caused by scrollbarint d = richTextBox1.GetPositionFromCharIndex(0).Y %                               (richTextBox1.Font.Height +1);    numberLabel.Location =new Point(0, d);    updateNumberLabel();}

Conclusion

I hope this user control helps developers handlingRichTextBox. I appreciate your advice and improvements to this control.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be foundhere

About the Author

Petr Minarik

Web Developer

Czech Republic Czech Republic

Member
Petr Minarik is currently studying at Czech Technical University Prague. He is interested in C# programming, Direct3D and graphics generally and he develops his own realtime 3D scene editor. He likes fun, beer and good people.
 
You can visit him at hishomepage.

loading...
Sign Up to vote  PoorExcellent
Add a reason or comment to your vote:x
Votes of 3 or less require a comment

Comments and Discussions

 
 RefreshFirstPrevNext
QuestionAnother one control to trymemberantgraf10:17 30 Oct '11  
GeneralMy vote of 1memberMember 45174973:58 22 Jul '10  
GeneralUncode supportmemberaldycool1:16 7 Apr '10  
GeneralWidth auto-adjustment and better management of first line offset and last existing line in RichTextBoxmemberMember 154992110:56 3 Jul '09  
QuestionRe: Width auto-adjustment and better management of first line offset and last existing line in RichTextBoxmemberzarawebfx4:11 8 Jan '10  
QuestionHow about indentmemberdfpcnc11:55 17 Jun '09  
GeneralDelete numbering lines of RichTextBox in c#memberSAADRAFID2:21 14 May '09  
GeneralProblems with ScrollmemberDiegoCol7:45 18 Jun '08  
QuestionSome problemsmemberChenQiang5:17 8 Dec '07  
GeneralRe: Some problemsmemberRodUbi8:31 11 Dec '07  
Last Visit: 19:00 31 Dec '99     Last Update: 9:13 29 Dec '1112345Next »

General General   News News   Suggestion Suggestion   Question Question   Bug Bug   Answer Answer   Joke Joke   Rant Rant   Admin Admin   

Permalink |Advertise |Privacy |Mobile
Web02 |2.5.111208.1 |Last Updated 3 Nov 2005
Article Copyright 2005 by Petr Minarik
Everything elseCopyright ©CodeProject, 1999-2011
Terms of Use
Layout:fixed|fluid

The Daily Insider

[8]ページ先頭

©2009-2025 Movatter.jp