Version 2.0!
Features
Tutorials
Files
Glossary
Projects
Contact
Links
Message Board
Extras
LuckyCam
Old News
Sign Guestbook
View Guestbook
VB Horoscope
VB Photo Album
.
ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!

Input Lines From a Text box  - By Gary Beebe

In this tutorial I'll show you how to get line inputs from a Text box (or String for that matter) and get a String from it, just as if the Text box was in a file and you were using the LineInput statement.  Getting inputs from a Text box has many advantages.  One reason is to save time.  You may find yourself editing a text file in Notepad, saving it, loading it into your game or editor and, testing the results.  You could, however, write your data to a text box and test the results.  By doing so, you wouldn't have to go through the time consuming opening and saving and opening.  You can just see your results, make a small change in the Text box and test the results again.  With my Function you can just substitute your LineInput statements with the Rline Functions.   Another good reason to do this is incase your program has to reformat the incoming data from a file and read it again.  If you have Encrypted save files you may have to unencrypt them to another file and read the valid information from it.  This permits the user's spying eyes find the Unencrypted file, which gives them a better chance at cracking your Encryption.  You can save yourself the embarrassment if you unencrypt the file to a Text box, which is harder to find in memory.  All of which, are reasons I chose to do this.

Sure, reading from Text boxes can be easy.  But, if you are like me, a game programmer, who rarely deals with Text boxes and detailed strings, may find that he/she is wasting a lot of time getting single lines from a Text box.  And the data may not be as accurate as you'd like.

Well "Here I come to save the day".  I have taken the liberty, after I have spent a couple hours to figure this out, to write a tutorial explaining how I did it.

I first, wrote my main Function.  It goes like this.

Public Function Rline(Text As String, SPos As Integer, RetPos As Integer) As String

If SPos >= Len(Text) Then
    Let Rline = "EOF"
    Let RetPos = 0
    Exit Function
End If

Dim L   'The length of our search

If SPos = 0 Then Let SPos = 1

For L = 1 To Len(Text) - SPos + 1
  Let Rline = Mid(Text, SPos, L)
  If Right(Rline, 2) = Chr(13) & Chr(10) Then Exit For
Next

If L >= Len(Text) Then
    Let RetPos = 0
Else
    Let RetPos = SPos + Len(Rline)
End If

If Right(Rline, 2) = Chr(13) & Chr(10) Then
  Let Rline = Left(Rline, Len(Rline) - 2)
End If

End Function

Wow, That's a lot!  Most of it handles all the fixing up of the returned string for you.  To get the Function we pass the Text that we are searching through (ex: Text1.Text), the position that we are starting our search from (SPos) and a variable that will later tell us where the search ended (RetPos).  The Function itself will return the line found in the Text.

The first thing you may notice is an IF statement that may seem unnecessary at the time.  This probably won't be necessary the first, second, or third time you run the function but if you keep running it, it will come in handy.  I'll explain that a bit more later.

The next thing you see is the Declaration of a variable, simply called LL is the variable that we are going to use to let us know where we are in the Text box.  This is important to know incase we try to search beyond the length of our Text box.  By keeping track of L, we reduce the risk of getting an error.

The next line also reduced the risk of getting an error.  Because SPos may be a zero and we can't search a string at a nonexistent position, we set it to 1 (one).

The For...Next loop starts the search from the start position (SPos) and can only continue as far as the distance from the start position to the end of the Text.  Inside the loop we keep adding the next character to our line.  When a line break is found (Chr(13) & Chr(10)) we prematurely exit the loop.  If a line break is not found then we exit the loop when the end of the text has been reached.

The If...Then...Else statement determines where the search and line ended and if the end of the Text was reached.

The last lines make sure the line break is taken out of our returned line, if there is one.

The next thing you need to do is declare some variables.  You may want to change them to Public variables if you are declaring them outside the Form or Module that you will be calling to the Function from.  They are as shown:

Dim MyLine As String
Dim BeginOfSearch As Integer
Dim EndOfSearch As Integer

And execute the function as follows, with those variables:

MyLine = Rline(Text1.Text, BeginOfSearch, EndOfSearch)

MyLine will be set the the next available line in the text.
Text1.Text can be replaced with the Text box or String that you are getting the lines from.
BeginOfSearch tells the function where to start searching for the next line.
EndOfSearch will become the position in Text1.Text (or whatever) where the search as ended.

You are now ready to work with MyLine as if it came strait from an external file.

If you have started at the beginning or somewhere in the middle of the Text box and wish to continue on to the next line you just set the beginning of your next search to the end of the last search:

Let BeginOfSearch = EndOfSearch

And just simply call the function again...

MyLine = Rline(Text1.Text, BeginOfSearch, EndOfSearch)

If and when you have reached past the end of the Text box, you will be notified.  The function will return the string "EOF" and the end of the search (EndOfSearch) will be set to zero, the next time you try to get input form the Text box.  This is all done by the first couple of lines, which seemed pointless at the time.

And there you have it.  I encourage you to use this function if it will save you any amount of time or help your program seem more professional to those people searching your work for a flaw.

Note: There is only one flaw. If your last line only contains a single character it will not appear. If this is your case then I suggest that you add a line to the end that you may use to determine the end of the text box.