Board index » delphi » check if a runtime-created component is ready/finished

check if a runtime-created component is ready/finished

Hi,
I have a problem here with some components that I am
creating at runtime.

I want to clear lines in a new RichEdit so I do:

with TRichEdit.Create(Self) do
begin
  Parent := SomeParent;
  Align := alClient;
  Lines.Clear; // This line here is the problem
  Name := 'NewRichEdit';
end;

It must takes some time until the creation has finished and I can
do something with the new component.
The 'Lines.Clear' gives me an WIN-API-exception.
(I think because the creation of the component hasn't finished yet)
So I need some way to wait until creation has finished and
the new component is ready for action.

Can anyone help me out?

Thanks in advance
Siegfried Hildebrand.

 

Re:check if a runtime-created component is ready/finished


In article <47kk58.ug7...@stargate.nixcomp>, Siegfried Hildebrand
wrote:

Quote
> I have a problem here with some components that I am
> creating at runtime.

> I want to clear lines in a new RichEdit so I do:

> with TRichEdit.Create(Self) do
> begin
>   Parent := SomeParent;
>   Align := alClient;
>   Lines.Clear; // This line here is the problem

Remove this line, it serves no purpose since the control is created
empty.

Quote
> It must takes some time until the creation has finished and I can
> do something with the new component.
> The 'Lines.Clear' gives me an WIN-API-exception.
> (I think because the creation of the component hasn't finished yet)

The window handle for it may not have been created yet (if the parent
is not visible, for instance), you can force that by calling
HandleNeeded.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Sent using Virtual Access 5.00 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Re:check if a runtime-created component is ready/finished


If you are creating the component at run time, don't name it.  If you
don't name it, you wont have any lines in the text.

This is what you want:

with TRichEdit.Create(Self) do
begin
  Parent := SomeParent;
  Align := alClient;
end;

-Gabe

Quote
Siegfried Hildebrand wrote:
> Hi,
> I have a problem here with some components that I am
> creating at runtime.

> I want to clear lines in a new RichEdit so I do:

> with TRichEdit.Create(Self) do
> begin
>   Parent := SomeParent;
>   Align := alClient;
>   Lines.Clear; // This line here is the problem
>   Name := 'NewRichEdit';
> end;

> It must takes some time until the creation has finished and I can
> do something with the new component.
> The 'Lines.Clear' gives me an WIN-API-exception.
> (I think because the creation of the component hasn't finished yet)
> So I need some way to wait until creation has finished and
> the new component is ready for action.

> Can anyone help me out?

> Thanks in advance
> Siegfried Hildebrand.

Re:check if a runtime-created component is ready/finished


Hello,

Quote
> >   Lines.Clear; // This line here is the problem

> Remove this line, it serves no purpose since the control is created
> empty.
> The window handle for it may not have been created yet (if the parent
> is not visible, for instance), you can force that by calling
> HandleNeeded.

Sorry that didn't help. Lines.Clear is not the only thing I want to do
with the RichEdit.
I want to put in some other otions and make Lines.Add('test'), too,
ans quick as possible.
Again, I want do this (now comlete ;-):

with TRichEdit.Create(Self) do
begin
  Parent := SomeParent;
  HandleNeeded;  // is that correct here? - Didn't help.
  Align := alClient;
  Lines.Clear;  // fails
  PlainText := true;  // fails
  ReadOnly := true;  // fails
  ScrollBars := ssVertical;  // fails
  Lines.Add('test');   // fails
  Name := 'NewRichEdit';
end;

So, WHEN can I do the above to the new RichEdit without
getting an 'EWin32Error - Win-API function failed'??

Thanks,
Siegfried Hildebrand

Re:check if a runtime-created component is ready/finished


In article <vivm58.sh...@stargate.nixcomp>, Siegfried Hildebrand
wrote:

Quote
> with TRichEdit.Create(Self) do
> begin
>   Parent := SomeParent;
>   HandleNeeded;  // is that correct here? - Didn't help.
>   Align := alClient;
>   Lines.Clear;  // fails
>   PlainText := true;  // fails
>   ReadOnly := true;  // fails
>   ScrollBars := ssVertical;  // fails
>   Lines.Add('test');   // fails
>   Name := 'NewRichEdit';
> end;

Siegfried,

i tried your code (just replacing Someparent with panel1) in a
buttons OnClick handler and the forms OnCreate handler and both
work without problem for me. (D5, Win95B w. IE5, simple new project
with a button and a panel on the form). Where exactly do you try to
execute this code?

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Sent using Virtual Access 5.00 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Re:check if a runtime-created component is ready/finished


Hello Peter,

Quote
> i tried your code (just replacing Someparent with panel1) in a
> buttons OnClick handler and the forms OnCreate handler and both
> work without problem for me.

Yes, you're right. Works for me, too with a OnClick handler
(Same procedure, using D5 with Win98SE)

Quote
> Where exactly do you try to execute this code?

I'm using (a NMMsg) and a NMMSGServ-Component in my program.
When a message comes in, the NMMSGServ OnMSG handler is called:

procedure TMainForm.MsgServerMSG(Sender: TComponent; const sFrom, sMsg:
String);
begin
  ReceiveMessage(sFrom, sMsg);
end;

It calls my 'TabSheet+RichEdit-Creation-Procedure' ReceiveMessage,
wich trys to handle the incoming message.

The ReceiveMessage-procedure makes the problem:
(When I use that procedure with some default-parameters from a
OnClick handler the TabSheet and RichEdit are created correctly)
I think the TabSheet somehow isn't created _correctly_
Often the Error (Exception EWin32Error, A Win32-API function has failed,
and after that a Protection-fault) is created much later than it occurs,
e.g. at closing of the application.
The Exception even occurs when I just create the TabSheet.
(comment-out the TRichEdit-creation)
- 'From' is a correct Identifier

procedure TMainForm.ReceiveMessage(From, Msg: String);
var
c2 : integer;

begin
  with TTabSheet.Create(Self) do begin
    try
      Parent := PageControl1;
      PageControl := PageControl1;
      HandleNeeded;  // is that correct the correct position for this?
      Caption := From;
      c2 := PageIndex;
      Name := 'Room'+From;
    except
      Free
    end; // try-except
  end; // create TTabsheet
  with TRichEdit.Create(Self) do begin
    try
      Parent := PageControl1.Pages[c2];
      HandleNeeded;  // is that correct the correct position for this?
      Align := alClient;
      Lines.Clear;
      PlainText := true;
      ReadOnly := true;
      ScrollBars := ssVertical;
      Lines.Add('test if all works');
      Name := 'ChatList'+RoomID;
    except
      Free
    end; // try-except
  end;  // create TRichEdit
end; // procedure ReceiveMessage

I hope you can tell me what's going wrong here.
(Sorry for posting so much code)

Thanks in advance,
Siegfried Hildebrand.

Re:check if a runtime-created component is ready/finished


In article <adnv58.cl3...@stargate.nixcomp>, Siegfried Hildebrand
wrote:

Quote
> with TTabSheet.Create(Self) do begin
>     try
>       Parent := PageControl1;
>       PageControl := PageControl1;

Do not assign Pagecontrol1 to the Parent property of the tabsheet!
Only set the Pagecontrol property.

Since i have 0 experience with these components you are using i can
only guess that there may be a timing problem. To solve this you
can try to post (POstMessage) a user message to the form and create
the tabsheet and richedit in the handler for the user message. Of
course you need a method to pass the two string parameters with the
message. One way to do to that would be to create a Tstringlist on
the fly, add the two strings to it pass the stringlists reference
as a message parameter, typecast to integer. The receiver would
cast it back to Tstringlist, get the strings from it and then Free
the stringlist instance.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Sent using Virtual Access 5.00 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Other Threads