Solved

Component Properties read component

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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