Jump to content
xisto Community
Sign in to follow this  
CaptainRon

Finding The Current Line Number In A Text Box A very basic method of doing the deed.

Recommended Posts

Language: Visual Basic 6.0 (5.0/4.0)

 

Level: Beginner

 

Problem: How to find the current Line Number in a multiline text box?

 

Some times it is necessary for us to provide a text editor in our programs, or maybe a text viewer. The text box control has no built in method to find the current line number like RichTextBox. Hence we need to code it manually.

 

Solution: The simple concept of line numbers is the carriage return character, or simply the ASCII code 10. The line feed character has the ASCII code 13. In the code example I have used chr(10), you can also use vbCrLf in place of it.

 

Simple concept is to keep finding the number of new line characters that have appeared before the current cursor position. Adding one to it will give us the current line number.

 

On Error Resume Next       'This is required in order to bypass 'the error that comes when on first linefound = -1    'Initialize a variable to store the start position 'of last chr(10) foundLinenum = 1   'Initialize Linenum variable to 1, since in any 'case we have one linepos = Text1.SelStart    'Position from where searching beginsDo Until found = 0    found = InStrRev(Text1.Text, Chr(10), pos, 0)    If found = 0 Then Exit Do    Linenum = Linenum + 1    pos = found - 1LoopLabel1.Caption = Linenum

 

The first four statements are explained with remarks.

 

Don't think about the first line now. On the VB form we have two controls, Text1 (text box control) and Label1 (Label control). The line number will be shown in the label.

 

We initialize three variables, found, Linenum and Pos.

 

Linenum keeps getting incremented until the Do loop finally ends with placing the correct line number in it. Here is how it happens. Firstly we take a variable called found. This variable will hold the value returned by the InStrRev function. InStrRev function searches for a specified string in another string and returns its position. InStrRev functions does the searching in reverse direction from the position specified. Its sister function is StrRev which does searching in forward direction.

 

Syntax of InStrRev:

 

InstrRev(stringcheck, stringmatch[, start[, compare]])

We called the function as follows:

 

found = InStrRev(Text1.Text, Chr(10), pos, 0)

Text1.text is the string being searched and chr(10) is the string being searched. We take a new variable called pos, which signifies the starting postion for the search in the given string (i.e. text1.text). Initially pos is set as the current caret location, which is given by text1.selstart Pos is taken since when we find a new line char, its obvious that we will look for a new line char above of it. So we recall InStrRev with pos set to the previous value of found subtracted by 1.

 

So InStrRev will return 0 when it doesn't find the new line character. That will symbolize that we have counted all the lines above the current cursor position. So we start a Do Loop which will loop until found = 0. The InstrRev function is the first statement inside the loop, since the searching has to be done until no more newline characters are found. If 0 is returned by InStrRev then Exit Do (quit the loop). Now if we are past this line that means that InStrRev did find a newline char and its position is stored in found. Since we no a newline char was found we increment Linenum by 1. Now we set the value of pos to value of found minus 1. To understand why, suppose the location of new line char came to be 23. So found = 23. Its obvious that if there is another new line char it will be behind the current location of the new line char. So we subtract found by 1 and put it in pos. The loop ends with Loop statement.

 

Now this Loop block will count all the line numbers above the current caret location and store it in Linenum. All we need to do is use this value and show it to the user. We use a Label in this case. So the simple statement goes as label1.caption = Linenum

 

There you go! Although this is very basic operation, and Rich Text Box has inbuilt method to do it. But suppose your application takes input from the user as the line number, and you are supposed to show that particular line in the Rich Text Box, then in that case even the inbuilt method doesn't come in a lot of use, since it is extremely slow at doing that. Whereas this method can be easily tweaked to perform the same.

 

With this article you also learn a practical use of the InStrRev.

 

 

Notice from miCRoSCoPiC^eaRthLinG:
Original article by the same author can be found at: http://forums.xisto.com/no_longer_exists/.

 

Next time make sure you enclose the blocks of code between CODE or CODEBOX tags.


Edited by miCRoSCoPiC^eaRthLinG (see edit history)

Share this post


Link to post
Share on other sites

how to select a purticular line in rich text box,

Finding The Current Line Number In A Text Box

 

I am trying to develop an editor for java , I am trying to provide a break point option so that the user can use in debugging ,

 

How can I select a particular line when a user click on that line and the line should be highlighted

 

Please help me out

 

-karthik

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.