$doctitle = "Determining the Active Field Name"; // Set this to your name; to include email, follow the format shown and use single quotes $authname = 'Lance Leonard'; // Additional attribution, e.g. your source or what you based this on. $attrinfo = ""; // 0=All, 1=New, 2=Intermediate, 3=Expert, 4=Developers Only $audience = "2"; // Version information. Use complete display string, e.g. Applies to v9.0 $versions = "Applies to: All versions"; // Date Posted; should be current date. $postdate = "26 September 2000"; // 0=Hides block, 1=About, 2=Paradox, 3=Delphi, 4=Assorted, 5=Web Stuff $pagearea = 3; // Navlinks provide the Section » Category Links. Uncomment the appropriate // line and delete the others. ## If this is an index page, set $navlinks to "" $navlinks = 'Delphi'; // Define your META Keywords here $metakeys = "delphi borland inprise data field name active current selected"; // Define the META description here; should be a simple, short sentence $metadesc = "One way to determine the name of the field bound to the active control"; ?> // this block is required include( $DOCUMENT_ROOT . "/lib/pageinit.php" ); ?>
Answer: Assuming that you're already verified that an object is data-aware, the following code shows one way to do this:
function getFieldName : String;
begin
with screen do
begin
if ( activeControl is tDBGrid ) then
result := tDBGrid( activeControl ).selectedField.fullName
else if ( activeControl is tDBEdit ) then
result := tDBEdit( activeControl ).dataField
else if ( activeControl is tDBCheckbox ) then
result := tDBCheckbox( activeControl ).dataField
else if ( activeControl is tDBMemo ) then
result := tDBMemo( activeControl ).dataField
else
raise exception.create(
'Can''t Get Field Name' + #10#10 + 'Reason: ' +
'An unexpected object called getFieldName().' + #10 +
'The activeControl is named ' + activeControl.Name );
end; // with
end;
This works by carefully testing Screen.ActiveControl against a list of object types used in the application. When a match is found, the C-style typecast ensures that Delphi will treat ActiveControl as the appropriate type of object when resolving the property reference leading to the field name.
Remember that the C-style typecasting (tDBGrid( activeControl), for example) is not the preferred way to typecast objects. The following snippet an approach generally recommended by the experts:
with ( ActiveControl as tDBGrid ) do
result := selectedField.fullName
This is considered a superior approach because it raises an exception if the object cannot be cast as the desired class. The original code works for many cases, however, because the if statements do not return true unless activeControl can be treated as the class being tested. Different developers have different tastes in this area, so use the approach that matches your personal preferences or development standards.
Also, it is possible to use RTTI to handle many of the objects, however, you'll still need additional tests for objects that do not have DataField properties, e.g. tDBGrids and many third-party data-aware components.
Finally, note that you'll need to add references to a valid USES block for this to work. Use the Help files to determine the appropriate unit to add.
// this block is required include( $DOCUMENT_ROOT . "/lib/pagecomp.php" ); ?>