Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
One Step Further: Examining the Sort Text Program Code
To add a few more tools to your programming skill set and review some of the concepts that I have discussed in the last several chapters, in the next exercise you'll take a closer look at the Sort Text program code.
Examine the Sort Text program
On the Sort Text program File menu, click the Exit command to stop the program.
Open the Code Editor, and display the code for the SortTextToolStripMenuItem_Click event procedure.
We've already discussed the first routine in this event procedure, which counts the number of lines in the text box by using the Substring method to search for carriage return codes. The remainder of the event procedure dimensions a string array, copies each line of text into the array, calls a procedure to sort the array, and displays the reordered list in the text box.
The entire SortTextToolStripMenuItem_Click event procedure looks like this:
Dim ln, curline, letter As String Dim i, charsInFile, lineCount As Short 'determine number of lines in text box object (txtNote) lineCount = 0 'this variable holds total number of lines charsInFile = txtNote.Text.Length 'get total characters For i = 0 To charsInFile - 1 'move one char at a time letter = txtNote.Text.Substring(i, 1) 'get letter If letter = Chr(13) Then 'if carriage ret found lineCount += 1 'go to next line (add to count) i += 1 'skip linefeed char (typically follows cr on PC) End If Next i 'build an array to hold the text in the text box ReDim strArray(lineCount) 'create array of proper size curline = 1 ln = "" 'use ln to build lines one character at a time For i = 0 To charsInFile - 1 'loop through text again letter = txtNote.Text.Substring(i, 1) 'get letter If letter = Chr(13) Then 'if carriage return found curline = curline + 1 'increment line count i += 1 'skip linefeed char ln = "" 'clear line and go to next Else ln = ln & letter 'add letter to line strArray(curline) = ln 'and put in array End If Next i 'sort array ShellSort(strArray, lineCount) 'then display sorted array in text box txtNote.Text = "" curline = 1 For i = 1 To lineCount txtNote.Text = txtNote.Text & _ strArray(curline) & vbCrLf curline += 1 Next i txtNote.Select(1, 0) 'remove text selection
The strArray array was declared in a module (Module1.vb) that's also part of this program (Chapter 10). By using the ReDim statement (Chapter 11), I am dimensioning strArray as a dynamic array with the lineCount variable. This statement creates an array that has the same number of elements as the text box has lines of text (a requirement for the ShellSort Sub procedure). Using a For loop (Chapter 7) and the ln variable, I scan through the text box again, looking for carriage return characters and copying each complete line found to strArray. After the array is full of text, I call the ShellSort procedure located in the Module1.vb module, which I created earlier in this chapter.
Display the code for the Module1.vb module in the Code Editor.
This module declares the strArray public array variable (Chapter 11) and then defines the content of the ShellSort procedure. The ShellSort procedure uses an If statement and the <= relational operator (Chapters 6, 8, and 13) to compare array elements and swap any that are out of order. The procedure looks like this:
Sub ShellSort(ByRef sort() As String, ByVal numOfElements As Short) Dim temp As String Dim i, j, span As Short 'The ShellSort procedure sorts the elements of sort() 'array in descending order and returns it to the calling 'procedure. span = numOfElements \ 2 Do While span > 0 For i = span To numOfElements - 1 For j = (i - span + 1) To 1 Step -span If sort(j) <= sort(j + span) Then Exit For 'swap array elements that are out of order temp = sort(j) sort(j) = sort(j + span) sort(j + span) = temp Next j Next i span = span \ 2 Loop End Sub
The method of the sort is to continually divide the main list of elements into sublists that are smaller by half. The sort then compares the tops and the bottoms of the sublists to see whether the elements are out of order. If the top and bottom are out of order, they're exchanged. The result is an array named sort() that's sorted alphabetically in descending order. To change the direction of the sort, simply reverse the relational operator (change <= to >=).
The remaining event procedures (OpenToolStripMenuItem_Click, CloseToolStripMenu-Item_Click, SaveAsToolStripMenuItem_Click, InsertDateToolStripMenuItem_Click, and ExitToolStripMenuItem_Click) are all similar to the procedures that you studied in the Text Browser and the Quick Note programs. (See my explanations earlier in this chapter for the details.)
Click the Close Project command on the File menu.
You're finished working with strings, arrays, and text files for now.
Congratulations! If you've worked through Chapters 5 through 13, you've completed the programming fundamentals portion of this book, and you are now ready to focus specifically on creating professional-quality user interfaces in your programs. You have come a long way in your study of Visual Basic programming skills and in your use of the Visual Studio IDE. Take a short break, and I'll see you again in Part III, “Designing the User Interface”!