Solved

Component Properties read component

Posted on 2001-08-06
5
254 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

22 Experts available now in Live!

Get 1:1 Help Now