| You are here: | About>Computing & Technology>Visual Basic> Using VB.NET> RichTextBox - It's Not Your Father's TextBox |
![]() | Visual Basic |
RichTextBox - It's Not Your Father's TextBoxNov 5 2007 RichTextBox is more than just a TextBox with more properties and methodsDr. Peter Zilahy Ingerman, a frequent contributor to these pages, asked a question that turned out to be so interesting that it deserved more than just a reply. It deserved this article. Peter asked: I have programmed a function that adds lines to a RichTextBox. I use this code to add lines: RichTextBox.AppendText(myTextString & vbCrLf) This much works just as I want it to. I need to write the code that will delete the top line of the RichTextBox. I attempted to use the following code: I = InStr(rtbStory.Text, vbCrLf) - 1 The problem is that InStr consistantly returns zero, saying that there aren't any "vbCrLf" strings. Clearly I am not understanding some elementary property of a RichTextBox ... and, equally clearly, Microsoft Help isn't helping for sour owl jowls. Right you are, Peter! The problem is that "Rich Text" is to "Text" as "differential and integral calculus" is to "arithmetic". And right you are again because your code works quite well with a regular text box. I coded up a little more complete example to prove it. Dim C As Char = "A" The illustration below shows a regular TextBox after five lines were added and two deleted. -------- But when the same code is used to add text to a RichTextBox, it doesn't work for exactly the reason Peter noted. If you examine the Text property of the RichTextBox, it just gets more confusing. The Text property just shows the actual characters in the RichTextBox: The reason is that the RichTextBox component converts what is really there into ASCII text and leaves out a lot, including the line break characters that Peter's code inserted. In fact, the line break characters were converted to rich text and don't actually exist in the text. That's why the If statement in Peter's code failed. What is really there can be displayed with the Rtf property: -------- Rich text can be used for highly formatted text and includes things like the font, the document margins, even pictures and other objects. For complete details, search MSDN for "Rich Text Format (RTF) Specification". (When I was in corporate data processing, I always recommended RTF as an editable document interchange format. Microsoft really did a masterful sales job in convincing the world to use Word instead!) It's quite possible to do something similar to Peter's code with a RichTextBox component, but you have to use the methods and properties of rich text to do it. There might be better ways (Let me know if you find one!), but this seems to work. Dim C As Char = "A" The Do loop crashes when there is no text in the component. It probably should be enclosed in a Try-Catch block. The GetLineFromCharIndex returns the line number of the line that the character that is indexed is in. So the second line can be found by simply incrementing until the next line is returned. After that, those characters are selected and then cut out of the document. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


