VB’s String-Handling Functions (The AutoCAD Object Model) (AutoCAD VBA)

Visual Basic provides several functions dedicated solely to handling strings: Len,UCase, LCase, Left, Right, Mid, and Trim, to name just a few. Let’s briefly define the role of some of the commonly used functions.

Len Function The Len function returns a Long type containing the number of characters in the string passed to it as an argument. If a variable is passed, Len will provide the number of bytes required to store the variable. If the variable represents a string, the result is exactly the same as the number of characters in the string, since each alphanumeric character is stored in a byte.

UCase and LCase Functions The UCase function changes to uppercase all the characters in the string passed to it. The LCase function changes them to lowercase characters. Both functions return a Variant containing the converted string.

Left and Right Functions The Left and Right functions return a specified number of characters from the left or right side of a string. The Left function has two required arguments: the string, followed by the number of characters you require for the leftmost substring that is returned as a Variant type. The Right function must be passed the string and the number of characters for the rightmost substring.

Mid Function The Mid function returns a specified number of characters from within a string. This function requires two arguments: the string itself, and the position within the string of the first character of the substring required. There is a third (optional) argument that lets you specify the length of the substring required. If this third argument is omitted, it is assumed you want to return all the characters from the first character to the end of the string.


Trim, LTrim, and RTrim Functions The Trim, LTrim, and RTrim functions all require a string passed to them as the argument. LTrim removes any leading blank spaces from the front of the string, RTrim removes any trailing blank spaces from the end of the string, and Trim removes any blank spaces from both ends of the string.

If the string passed to any of the string-handling functions contains NULL, they will return NULL.

In Exercise 5.4, you’ll create a GUI in which you can enter a string and watch the effect of some of the string-handling functions.

Exercise 5.4: String Handling Functions Application

1. Start a new project. Add a UserForm with eight text box controls, seven command buttons, and a label, as shown here:

tmp757e246_thumb

2.    Update the Caption properties of these controls as shown in Table 5.4.

3.    Enter the code for the skeleton event procedures for the command button controls as shown in Listing 5.6.

4.    Run your application, enter a string, and try clicking all the command button controls. Figure 5.16 shows my attempt at this!

Table 5.4 Caption Property Settings for the GUI Objects

Object

Caption

UserForm1

String Functions

Label1

Enter string

CommandButton1

Find length

CommandButton2

Uppercase

CommandButton3

Lowercase

CommandButton4

First four characters

CommandButton5

Last five characters

CommandButton6

Middle three

CommandButton7

Trim end spaces

Result of entering a text string and clicking all the command buttons

Figure 5.16 Result of entering a text string and clicking all the command buttons

Listing 5.6: Click Event Procedures for Command Buttons

Listing 5.6: Click Event Procedures for Command Buttons

Analysis

•    Line 1 starts the CommandButton1 click event procedure.

•    Line 2 calls the Len function with the text entered into TextBox1 as the argument and assigns the value returned to the Text property of TextBox2, which automatically displays the new text assigned to it in TextBox2.

•    Line 3 ends the CommandButton1 click event procedure.

•    Line 5 starts the CommandButton2 click event procedure.

•    Line 6 calls the UCase function, which changes all the characters of the string entered into uppercase and displays them in TextBox3.

•    Line 7 ends the CommandButton2 click event procedure.

•    Line 9 starts the CommandButton3 click event procedure.

•    Line 10 calls the LCase function, which converts the string entered in TextBox1 into lowercase and displays the converted string in TextBox4.

•    Line 11 ends the CommandButton3 click event procedure.

•    Line 13 starts the CommandButton4 click event procedure.

•    Line 14 calls the Left function with the text entered as the first argument and 4 as the second. The leftmost four characters are returned and displayed in TextBox5.

•    Line 15 ends the CommandButton4 click event procedure.

•    Line 17 starts the CommandButton5 click event procedure.

•    Line 18 calls the Right function with the text entered and 5 as the arguments. This returns the last five characters and displays them in TextBox6.

•    Line 19 ends the CommandButton5 click event procedure.

•    Line 21 starts the CommandButton6 click event procedure.

•    Line 22 calls the Mid function, which requires three arguments—a string,starting position, and the number of characters required. In Listing 5.6, the string entered is given as the first argument. The second argument is given as an expression containing a function call. The Len function returns the length of the string that is then halved and decremented to give the character before the middle of the string. The third argument is passed as the number 3 to determine the number of characters that will be displayed in TextBox7.

•    Line 23 ends the CommandButton6 click event procedure.

•    Line 25 starts the CommandButton7 click event procedure.

•    Line 26 calls the Trim function to trim any leading or trailing spaces from the string entered into TextBox1 and assigns the resultant string to TextBox8.

•    Line 27 ends the CommandButton7 click event procedure.

Summary

At the end of this topic, you’ll know the following about working with AutoCAD VBA:

•    The role of the Application object.

•    How the active document from the Application object can be represented by ThisDrawing.

•    What the Documents collection contains.

•    How to use the Item method to return an object from a collection.

•    About the MsgBox function and when to use either it or the Prompt method to send messages to the user.

•    How the On Error Resume Next statement can save your application from abnormal termination.

•    About the CInt function and how it returns a value from the ASCII character set.

•    Whether to use the End statement or the Unload statement.

•    The function of the QueryClose and Terminate event procedures.

•    The differences between the Model Space, the Paper Space, and the active space.

•    How to create and add graphical objects to your ModelSpace, PaperSpace, or Blocks collections.

•    How to access existing graphical objects.

•    How to use the Preferences object to set the options of AutoCAD’s Options dialog box.

•    How to send messages for the user to the command line.

•    How to use some of Visual Basic’s string-handling functions.

Next post:

Previous post: