Most of the properties for manipulating text in a TextBox control are available at runtime only. This section presents a breakdown of each property.
Text
The most important property of the TextBox control is the Text property, which holds the control’s text. You can set this property at design time to display some text on the control initially. Notice that there are two methods of setting the Text property at design time. For single-line TextBox controls, set the Text property to a short string, as usual. For multiline TextBox controls, open the Lines property and enter the text in the String Collection Editor window, which will appear. In this window, each paragraph is entered as a single line of text. When you’re finished, click OK to close the window; the text you entered in the String Collection Editor window will be placed on the control. Depending on the width of the control and the setting of the WordWrap property, paragraphs may be broken into multiple lines.
At runtime, use the Text property to extract the text entered by the user or to replace the existing text. The Text property is a string and can be used as an argument with the usual string-manipulation functions of Visual Basic. You can also manipulate it with the members of the String class. The following expression returns the number of characters in the TextBox1 control:
Dim strLen As Integer = TextBox1.Text.Length
Code language: VB.NET (vbnet)
The IndexOf method of the String class will locate a specific string in the control’s text. The following statement returns the location of the first occurrence of the string Visual in the text:
Dim location As Integer
location = TextBox1.Text.IndexOf("Visual")
Code language: VB.NET (vbnet)
Formore information on locating strings in a TextBox control, see the section “VB 2008 The TextPad Project” later in this chapter, where we’ll build a text editor with search-and-replace capabilities. For a detailed discussion of the String class, see Chapter, “Handling Strings, Characters, and Dates.”
To store the control’s contents in a file, use a statement such as the following:
StrWriter.Write(TextBox1.Text)
Code language: VB.NET (vbnet)
Similarly, you can read the contents of a text file into a TextBox control by using a statement such as the following:
TextBox1.Text = StrReader.ReadToEnd
Code language: VB.NET (vbnet)
where StrReader and StrWriter are two properly declared StreamReader and StreamWriter variables. File operations are discussed in detail in Chapter, “Accessing Folders and Files.” You will also find out how to print text files in Chapter, “Printing with Visual Basic 2008.” To locate all instances of a string in the text, use a loop like the one in Listing 6.1. This loop locates successive instances of the string Basic and then continues searching from the character following the previous instance of the word in the text. To locate the last instance of a string in the text, use the LastIndexOf method. You can write a loop similar to the one in Listing 6.1 that scans the text backward.
Listing 6.1: Locating All Instances of a String in a TextBox
Dim startIndex = -1
startIndex = TextBox1.Text.IndexOf("Basic", startIndex + 1)
While startIndex > 0
Console.WriteLine "String found at " & startIndex
startIndex = TextBox1.Text.IndexOf("Basic", startIndex + 1)
End While
Code language: VB.NET (vbnet)
To test this code segment, place a multiline TextBox and a Button control on a form; then enter the statements of the listing in the button’s Click event handler. Run the application and enter some text on the TextBox control. Make sure that the text contains the word Basic or change the code to locate another word, and click the button. Notice that the IndexOf method performs a case-sensitive search.
Use the Replace method to replace a string with another within the line, the Split method to split the line into smaller components (such as words), and any other method exposed by the String class to manipulate the control’s text. The following statement appends a string to the existing text on the control:
TextBox1.Text = TextBox1.Text & newString
Code language: VB.NET (vbnet)
This statement has appeared in just about any VB 6 application that manipulated text with the TextBox control. It is an inefficient method to append text to the control, especially if the control contains a lot of text already. Now, you can use the AppendText method to append strings to the control, which is far more efficient than manipulating the Text property directly. To append a string to a TextBox control, use the following statement:
TextBox1.AppendText(newString)
Code language: VB.NET (vbnet)
The AppendText method appends the specified text to the control as is, without any line breaks between successive calls. If you want to append individual paragraphs to the control’s text, you must insert the line breaks explicitly, with a statement such as the following (vbCrLf is a constant for the carriage return/new line characters):
TextBox1.AppendText(newString & vbCrLf)
Code language: VB.NET (vbnet)
Lines
In addition to the Text property, you can access the text on the control by using the Lines property. The Lines property is a string array, and each element holds a paragraph of text. The first paragraph is stored in the element Lines(0), the second paragraph in the element Lines(1), and so on. You can iterate through the text lines with a loop such as the following:
Dim iLine As Integer
For iLine = 0 To TextBox1.Lines.GetUpperBound(0) - 1
{ process string TextBox1.Lines(iLine) }
Next
Code language: VB.NET (vbnet)
You must replace the line in brackets with the appropriate code, of course. Because the Lines property is an array, it supports the GetUpperBound method, which returns the index of the last element in the array. Each element of the Lines array is a string, and you can call any of the String class’s methods to manipulate it. Just keep in mind that you can’t alter the text on the control by editing the Lines array. However, you can set the control’s text by assigning an array of strings to the Lines property.