Solved

Checking if Component has a Caption Property

Posted on 1997-05-27
2
1,802 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
[X]
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
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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