Borland Online And The Cobb Group Present:


April, 1995 - Vol. 2 No. 4

Borland C++ 4.0 Tip - Solving a problem with the TPXPictureValidator class

In last month's issue, we showed how you could use the TPXPictureValidator class to verify data in an entry field of a dialog box (Automatically verifying data entries with picture validators).Unfortunately, there's a slight problem when you try to use the autoFill option with this class.

If you enable the autoFill option for a TPXPictureValidator object, the validator should automatically insert any literal characters that are part of the validator's picture string. However, the TEdit and TPXPictureValidator classes, which are part of the ObjectWindows Library (OWL) 2.0 code (the version that ships with Borland C++ 4.0), don't interact correctly to automatically fill in the literal characters for you. In fact, in some cases, they won't even allow you to input data that's in the correct format.

Fortunately, Borland fixed this error in OWL 2.5 (which ships with Borland C++ 4.5). In this article, we'll show how you can fix this problem in the OWL 2.0 code by applying some of the changes to the TPXPictureValidator and TEdit classes that Borland made for OWL 2.5.

The problem

In OWL 2.0, the constructor for the TPXPictureValidator class includes the autoFill option as the second parameter. If you create a TPXPictureValidator object with the autoFill parameter set to TRUE, the validator should attempt to add literal characters from the picture string to the appropriate places in the edit control.

As you enter data into the edit control, the validator scans each character and compares it to the corresponding character position in the picture string. If the validator detects a literal character in the picture string, it should copy that character over to the edit control's character array. Instead, when the validator detects a literal character, it tries to change the address of the control's character array.

To understand what's happening, you should realize that many of the OWL classes use a form of String class to manipulate character arrays. At one point in OWL's development, the TPXPictureValidator class used a String object to internally represent and manipulate the edit control's character array information.

To append the literal character from the picture string, the TPXPictureValidator class's Picture( ) member function contained the line

input = input + Pic[i];

where input was a String parameter, Pic was a String object, and i was the position of the literal character in the picture string. Since the String class used the + operator to append characters, this worked well and tested correctly.

However, later in OWL's development, Borland decided to change the input parameter to a simple character array (probably to improve speed). Since the other uses of the input parameter in the source code were in locations that expected a character array (and those locations depended on the String class's conversion to a pointer to a char) and since adding values to character array addresses is legal, this change didn't generate any errors.

As you can imagine, though, the line shown above makes it impossible to enter valid data into an edit control that uses picture string validation and the autoFill option. Fortunately, the solution is simple to implement.

The solution

In order to fix this problem with the TPXPictureValidator class, you need to do two things: Make a slight change to the TPXPictureValidator class's Picture( ) member function and rebuild the OWL. Along the way, you should fix a problem with the TPXPictureValidator class's Group( ) member function and repair a minor bug in the TEdit class's EvChar( ) message response function.

In the TPXPictureValidator::Picture( ) member function, you'll change the line we mentioned earlier to perform a simple character-by-character copy from the picture string array to the edit control's array. This will transfer the appropriate literal characters­­one at a time­­to the edit control.

In the TPXPictureValidator::Group( ) member function, you'll change the code that sets the terminating character index to use the last character in that group. This fixes a subtle problem that occurs when you're attempting to validate alternate combinations of data (known as validation groups).

Finally, in the TEdit class's EvChar( ) message response function, you'll change the lines that set the insertion point after an autoFill operation. The existing code leaves the insertion point at the end of the data you entered, but before the literal characters the validator added to the edit control's text.

Picture-perfect OWL

To make these changes to the OWL library, launch the Borland C++ 4.0 Integrated Development Environment (IDE). When the IDE's main window appears, choose Open... from the File menu. In the Open A File dialog box, enter the pathname \BC4\SOURCE\OWL\PICTVAL.CPP and click OK.

When the editing window for this file appears, right-click in the window and choose Go To Line... from the pop-up menu. In the Go To Line Number dialog box, enter 384, and click OK.

Next, replace lines 384 through 391 (the current line number appears in the Status Bar) with the following code:

// Changes from the OWL 2.5 source
  input[j++] = Pic[i++];
  reprocess = TRUE;
}
if (reprocess) {
  input[j] = 0;
  j = i = 0;
  rslt = Process(input, Pic.length( ), i, j);
}
// End of OWL 2.5 changes

When you finish, the editing window for this file should resemble the one shown in Figure A. Choose Save from the File menu to save these changes.

Figure A - You'll make the necessary changes in the PICTVAL.CPP file.

Now, choose Exit from the File menu to quit Borland C++. Then, exit Windows by double-clicking on the Program Manager's System menu icon.

When the DOS prompt appears, make the OWL source code directory the current directory by entering

cd \BC4\SOURCE\OWL

From this directory, you can rebuild the OWL library. However, first you need to decide which version of the library you need to create.

In Table A, locate the OWL library version you need in the Type column, and then find the corresponding command-line syntax in the Make column. When you locate the correct version, enter the appropriate command to begin rebuilding the OWL library. (This process may take up to 15-20 minutes.)

Table A: Standard Make commands
Type Make
16-bit DLL make OWLNAME=Owl OWLVER=200
32-bit DLL make -DWIN32 OWLNAME=Owl OWLVER=200
32-bit memory model make -DWIN32 -DOWLSECTION
Small memory model make MODEL=s -DOWLSECTION
Medium memory model make MODEL=m -DOWLSECTION
Large memory model make MODEL=m -DOWLSECTION

If you're rebuilding one of the DLL versions of the OWL library, their MAKE commands will update both the DLL file and the OWL library interface file OWLWI.LIB. Because rebuilding the OWL library changes the OWLWI.LIB file, you'll need to re-link all of your OWL projects to allow them to use the new interface file.

Conclusion

The TPXPictureValidator class allows you to easily check the validity of user input. After fixing the problem with the OWL source code, you can also use the class TPXPictureValidator to automatically add literal characters to the edit control as the user enters the data.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.