Solved

Component Properties read component

Posted on 2001-08-06
5
259 Views
Last Modified: 2010-04-06
Another properties question.
I have a property to which I can assign a checkbox.
When the application starts I want to check this property and read its checked value.
So I test to see if the checkbox property is <> nil, in my components constructor, this always returns false. Later when the project is running I can read the checkbox without problems. Is this because my component is created before the checkbox?
0
Comment
Question by:sorentop
[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
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
nnbbb09 earned 50 total points
ID: 6357413
Hello,

Delphi creates the components on a form in the order in which they were added to the form, so it is very likely that your component could be created before the checkbox exists, so it is always a good idea to ensure it exists by using "assigned" or checking for nil. If you need to run initialization code for your component that depends on all controls on the form being present then override the Loaded procedure.

Jo
0
 

Author Comment

by:sorentop
ID: 6357426
Hi again

Is that something I can do from the component ?
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 6357636
Your component has most likely overridden the Create constructor already.  Overriding the Loaded procedure is very similar.

In the component class's Public section add:

procedure Loaded; override;

And in the implementation section you add:

procedure TMyClass.Loaded;
begin
     ReadCheckBoxCheckedValue;
     inherited;
end;

0
 
LVL 3

Expert Comment

by:nnbbb09
ID: 6358523
Hello,

The Loaded procedure is declared in TComponent and should be overriden in your class as MannSoft has shown. What happens is once all the components on your form have been created from the stream, each Components Loaded procedure is called.

Incidentally, if you are linking two components together at design-time using the Object Inspector then you will need to add some code to your component manage the deletion of the checkbox, otherwise you may get access violations. To do this you need to override the Notify procedure eg :

protected
   procedure Notification(AComponent: TComponent;
 Operation: TOperation); override;

procedure TMyClass.Notification(AComponent: TComponent;
 Operation: TOperation);
begin
 inherited Notification(AComponent, Operation);
 if (Operation = opRemove) and
    (AComponent = MyCheckbox) then MyCheckbox := nil;
end;

This means your component will be notified everytime a component on the same form is deleted. If it is the checkbox then set your Checkbox property to nil.

Jo
0
 

Author Comment

by:sorentop
ID: 6358684
OK I did not know the loaded procedure.

Also thanks MannSoft for your example.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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