Solved

Checking if Component has a Caption Property

Posted on 1997-05-27
2
1,547 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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now