Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Checking if Component has a Caption Property

Posted on 1997-05-27
2
Medium Priority
?
2,234 Views
Last Modified: 2013-11-23
I have a program that accesses varying components via the
TWinControl base class.  TWinControl does not have a caption
property.  I'd like to be able to set the caption property
if one exists for the component.

1) How do I check if a caption property exists for any control
   that can be selected?

2) What base class can I use that will cover all visible
   components, yet have a caption property?

3) If I have to use a try..except block...what exception will
   I have to trap?
0
Comment
Question by:mheacock
2 Comments
 
LVL 3

Accepted Solution

by:
sperling earned 200 total points
ID: 1336624
Two ways of doing this.

1. As TControl has a protected Caption property, you can derive a class from TControl which simply makes Caption public, and then cast any TControl to this new type and set the Caption.

2. Use the Delphi RTTI (Run-Time Type Information) functions to check whether any object has a Caption property, and if so set it.

uses
  TypInfo;

type
  TControlFix=class(TControl)
  public
    property Caption;
  end;

procedure SetControlCaption1(Ctrl : TControl; value : STRING);
begin
  TControlFix(Ctrl).Caption := value;
end;

procedure SetControlCaption2(Ctrl : TControl; value : STRING);
var
  PropInfo  : PPropInfo;
begin
  PropInfo := GetPropInfo(Ctrl.ClassInfo, 'Caption');
  if PropInfo<>nil then begin
    SetStrProp(Ctrl, PropInfo, value);
  end;
end;


Note that the first function will set the Caption of any TControl descendant, regardless of whether the Caption is protected in the actual class. It will however not modify "new" Caption properties, only the original TControl.Caption. Also, this function will set the Text property of a TEdit, as this actually is the caption of an edit.

The second function will set the Caption if the Caption is published only. It'll not affect TEdit.

Regards,

Erik.
0
 
LVL 3

Author Comment

by:mheacock
ID: 1336625
The number 2 of your question was exactly what I was looking for.
I didn't really want to derive any classes...I received various
comments to do this...seemed kind of a hack to me...the RTTI
stuff seems the better route.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question