Search and Replace Operations

The last option in the Edit menu -and the most interesting-displays a Search ‘& Replace dialog box (shown. in Figure 5.5). This dialog box wor ks like the Find and Replace dialog box of Word and many other Windows applications.

The buttons in the Search & Replace dialog box are relatively self-explanatory: Find Locates the first instance of the specified strir.g in the text. In other words, Find starts searching from the beginning of the text, not from the current location of the pointer. If a match is found; the Find Again, Replace, and Replace All buttons are enabled. Find Again Locates the next instance of the string in the text Replace Replaces the current instance of the found string with the ‘ replacement string and then locates the next instance of the same string.

Replace All Replaces all instances of the string specified in the Find String box with the smngin the Replace With box ” Whether the search is case-sensitive depends on the status of the Case Sensitive

Check Box control The Find command checks the sta~s of this Checkbox and sets the compare variable accordingly. This variable is then used with the InStrO function to specify the type of search.

The actual search is performed with the InStr () function, which searches thetext in the TextBox control for the specified string. If the string is found, the program highlights it by selecting it and then stores its position in the position variable. The position variable is declared as a Form-wide variable so that it _an be used later by the Find Again button to specify the starting location of the new search.

The TextPad Application’s Find Button

The Find button examines the value of the Checkl CheckBox control, which specifies whether the search will be case-sensitive and sets the value of the compare variable accordingly. The compare variable is passed to the InStrO function and the variable tells it how to search for the desired string. If the InStr() function locates the string, the program selects it by setting the textbox’s SelStart and SeILength properties. If not, it displays a message and disables the Replace and Replace All buttons on the Form

The code of the Find Again button is the same, but it doesn’t reset the position variable to zero. This way, the InStr() function locates the next instance of the same string. “

The Replace button replaces the current selection with the replacement stringand then locates the next instance of the find string. The Replac e All button, does the same thing as the Replace button, but it continues to replace the found string until no more instances can be located in the text. The code behind the Find Again. Replace; and Replace All buttons is quite similar to the code of the Firnd button and really doesn’t need repeating. If you open the TextPad sample application, you can see the differences. When you have the application open, consider modifying the code so that the Find button starts searching for the string from the current pointer’s location. To do this, pass the pointer’s location as the first argument to the InStrO function

You might also want to limit the search operation to the selected text only. To do so, pass the location of the first selected character to the InStr() function as before. In addition, you must make sure that the located string falls within the selected range, which is from Forml. Editor. Se1Start to Form!.Editor.

Captl:lri.lg Keystrokes

The TextBox control has no unique methods or events, but it’s quite common in programming to use this control to capture and process the user’s keystrokes. The KeyPress event occurs every time a key is pressed, and it reports the character that was pressed. You can use this event to capture certain keys ~nd modify the’program’s behavior; depending on the character typed.

Suppose you want to use the·TextPad ‘application (discussed earlier) to prepare messages for transmission over a telex line. As you may know, a telex can’t transmit lowercase characters or special symbols. The editor must convert the text to uppercase and replace the special symbols with their equivalent strings: DLR for $, AT for@, % for %, BPT for #, and AND for &. You can modify the)default behavior of the TextBox control from within the Keyf’ress event so that it converts these characters as the user types.

The TLXPad application is identical to the TextPad application, but customized for preparing telex messages. (Not that the telex is growing in popularity, but there are situations in which some custom preprocessing of the data is required.) By capturing keystrokes, you can process the data as they are entered, in real time. For .example, you could make sure that numeric values fall within a given range or that hexadecimal digits don’t contain invalid characters, and so on. The only difference is the modified application’s KeyPress event. The KeyPress event handler of the TLXPad application is shown next.

TLXPad Application’s KeyPress Event Handler

Capture

This event Iiandler replaces the special symbols with a string, and it also converts alpha-numeric characters to uppercase. The line that coriverts the alphanumeric characters to uppercase needs some explanation. The KeyAscii argument of the Keyl’ress event reports the ASCII value of the key presses, not the actual character:

KeyAscii – Asc(UCaseS(ChrS(KeyAscii»)

Before deploying the UCase$() function to convert the character to uppercase, you must convert the II value to acharacter, This is what the function Chr$() in the preceding long statement does. To understand it, read it from right to left: first it extracts the actual character typed from its ASCII value; then it converts the character to uppercase; and finally, it converts the.character back to an ASCII
value, which is returned to the application. The KeyPress event handler is executed before the keystroke is passed to the TextBox control, and that’s why you can modify the keystrokes’ from wi~ the control’s KeyPreSs event.

Capturing Function Keys

Another common feature in text-editing applications is the assignment of special operations to the function keys The Notepad application, for example, uses the FS function key to insert thecurrent date at the cursor’s location. You can do the same with the TextPad application, but you can’t use the KeyPress event-the  KeyAscii argument doesn’t report function keys. The events that can capture the function keys are the Keybown event, which is generated when a key is pressed, and the KeyUp event, which is generated when a key is released. Also, unlike the KeyPress event, the KeyDown and KeyUp events don’t report the ASCII value of the character, put instead, report its keycode (a special number that distinguishes earp key on the keyboard, also known as the scancode).

The keycode is unique for each key, not each character. Lower- and uppercase characters have different ASCII values but the same keycode because they are on the same key. The number 4 and the $ symbol have the same keycode because the same key on the keyboard generates both characters, When the key’s keycode is reported, the KeyOown and Keyl.Ip events also report the slate of the Shift, Control, and Alt keys.

To be able to use the KeyDown and KeyUp events, you must know the keycode of the key you want to capture. The keycode for the function key FI is 112 (or the constant pbKeyF12), the keycodefor F2 is 113 (or the constant vbkeyF13), and so on. To capture a special key, such as the FI function key, and assign a special string to it, , program the key’s KeyUp event. The following event handler uses the FS and F6 function keys to insert the current date and time in the document. It also uses the F7 and F8 keys to insert two predefined strings  the document.

Scroll to Top