Solved

Checking if Component has a Caption Property

Posted on 1997-05-27
2
1,709 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 50 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 62
drawing animated level bar based on numbers 3 98
how to resize animated Gif image in delphi ? 1 31
Graphics32 under Delphi 10.1 Berlin 2 69
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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