Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Checking if Component has a Caption Property

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

765 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