Solved

Form Show (for 150 points)

Posted on 2001-07-12
16
173 Views
Last Modified: 2010-04-04
Hi experts,

I'm depositing 25 points for this question but if it gets answered satisfactorily  I'll give out 150 points.

My code has been acting rather weird (at least as it appears to me) lately.  I set up three breakpoints in the code, one in the main form's call to create of another form (Breakpoint A),  another in call to show the form(Breakpoint B), and another in the form's OnShow handler
(Breakpoint C).  See the code below.  My problem is Breakpoint C is reached before breakpoint B.  As you can see in the code, the OnSHow handler of MyFrm calls a procedure that  accesses MyList, a property in MyFrm.  At Breakpoint C, MyFrm is still nil therefore an error is generated when the AddItem procedue is called.

procedure TMainFrm.ChartsExecute(Sender: TObject);
begin
  if not Assigned(MyFrm) then
    MyFrm:=TMyFrm.Create(application);{breakpoint here A}
  MyFrm.Show;{breakpoint B}
end;

procedure TMyFrm.FormShow(Sender: TObject);
begin
   AddItem; {breakpoint C,  AddItem is a function defined in another unit}
end;


//another unit :
procedure AddItem
begin
  Memo.Lines.Add(MyFrm.MyList.Items[1]);
end;


  Below is the definition of TMyFrm :

  TMyFrm = class(TMy2Class)
    MyList1: TMyList;{a component I derived from TList}
  private
  public
  end;

  TMy2Class is a class derived from other classes derived from TForm,
  something like :
    TMy2Class= class(TMy1Class)
    TMy1Class= class(TForm)

The contents of the CallStack upon reaching Breakpoint C is as follows:

TMyFrm.FormShow
TCustomForm.DoShow
TCustomForm.CMShowingChanged
TCustomForm.WndProc
TCustomForm.WndProc
TCustomForm.AfterConstruction
TMainFrm.ChartsExecute

This doesn't happen in the other forms.   One of the hyphothesis that I have is that MyFrm was not derived directly from TForm.



  Cheers!
  -Tony
0
Comment
Question by:TheBreeze
  • 4
  • 4
  • 3
  • +3
16 Comments
 
LVL 6

Accepted Solution

by:
Jaymol earned 100 total points
ID: 6276004
Instead of having "AddItem" in the FormShow event, simply run it directly after the MyFrm.Show command.

Worth a go.

John.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6276008
try to use active event instead of show event (for break c)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6276124
I guess TMyFrm.Visible is true in the object inspector. Set it to false. This should solve the problem.

Regards, Madshi.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 3

Expert Comment

by:nnbbb09
ID: 6276172

Hello,

Is the visible property of TMyFrm set to true. If it were it would cause the problems that you are experiencing. By default it should be false, but you never know...

Jo
0
 
LVL 3

Expert Comment

by:nnbbb09
ID: 6276174

Oops. Ignore my comments Madshi got there first.
0
 

Author Comment

by:TheBreeze
ID: 6276180
TMyFrm.Visible is set to false
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6276255
Hello

  try as I told you before, to change the code from the event show to active, the event active execute when the form draw in the screen
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6276298
Hmmmm... Then maybe the TMyFrm.OnCreate handler set TMyFrm.Visible to true? Or calls Show? Or something like that? Someone has to do that somewhere, otherwise your breakpoint would not be "confused".

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6276300
P.S: Why not stepping line by line through the code? Then you should see from where the code jump to the TMyFrm.FormShow handler.
0
 

Author Comment

by:TheBreeze
ID: 6276340
TMyFrm.Visible is set to false
0
 

Author Comment

by:TheBreeze
ID: 6276380
mnasman,
I'm not quite sure what you mean.  Do you mean I should use MyFrm.Active:=true instead of MyFrm.Show?

madshi,
I traced the program and right after the form create
procedure the code jumped to the following :

procedure TCustomForm.DoCreate;
begin
  if Assigned(FOnCreate) then
  try
    FOnCreate(Self);
  except
    Application.HandleException(Self);
  end;
  if fsVisible in FFormState then Visible := True;
end;

For additional info, as I have already written above:
The contents of the CallStack upon reaching Breakpoint C are as follow:

TMyFrm.FormShow
TCustomForm.DoShow
TCustomForm.CMShowingChanged
TCustomForm.WndProc
TCustomForm.WndProc
TCustomForm.AfterConstruction
TMainFrm.ChartsExecute

Experts, thank you for your time.  Got to go home now.  I'll be checking in again tomorrow.

0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6276390
no, i don't mean to change the event from onshow to onactivate

procedure TMyFrm.FormShow(Sender: TObject);
begin
  AddItem; {breakpoint C,  AddItem is a function defined in another unit}
end;


procedure TMyFrm.FormActivate(Sender: TObject);
begin
  AddItem; {breakpoint C,  AddItem is a function defined in another unit}
end;

also I think you should remove the MyFrm from the autocreate opetion from projects menu, cuz u create it in run time and you don't need delphi to create it for you

to remove it from the Auto-create

goto to project menu > options
remove MyFrm from auto-create forms to avaible forms
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6276396
Okay, there we have it! It seems that "FFormState" contains the "fsVisible" flag. The big question is: How does the flag come there? There's only one possibility: Look at the code from "TCustomForm.SetVisible" in the "forms" unit. That's the only location where the "fsVisible" flag is set. That means, someone is calling SetVisible of your TMyFrm (or setting "Visible := true"). My suggestion: Set a breakpoint to "TCustomForm.SetVisible". If you get there, look at the callstack and go through it, then you should find the guilty one, who makes your TMyFrm visible.

Regards, Madshi.
0
 
LVL 3

Expert Comment

by:rondi
ID: 6279716
It appears to me that your problem is two-fold.
First, as Madshi pointed out, someone's messing with
your form behind your back;
but secondly and more importantly, when that someone
does show your form, there shouldn't be an access violation
if MyList has been created.

Make sure MyList is created and initialized BEFORE the FormShow event. You could create the list in MyFrm.FormCreate, this way even if someone else does create
and show your form, the list will be ready to receive new
items anyway.

HTH,
rondi
0
 

Author Comment

by:TheBreeze
ID: 6279786
mnasman,
-I don't think FormActivate would do
-MyFrm is not autocreate

Madshi,
I took your advice and the editor took me deep into the Forms unit without seeing any light.
Anyway, I think the "error" is brought about by the fact that TMyFrm was derived from other classes.

rondi,
MyList is an installed user component which is added to MyFrm.  I don't think it should still be created

Actually the solution proposed by Jaymol works.  I removed the OnShow event handler and transfered the code to another procedure, and called that procedure after creating the form.  

procedure TMainFrm.ChartsExecute(Sender: TObject);
begin
  if not assigned(ChartFrm) then
    ChartFrm:=TChartFrm.Create(application);
  ChartFrm.MyCustomShow;
end;

I didn't want to accept it as a solution in the beginning because it is just a workaround - it doesn't really explain why breakpoint B is reached before breakpoint C. The solution proposed by Madshi may work, but it would probably take more time before I figure out how to figure it out.  
Anyway, I guess since it's the solution of Jaymol that I will implement, he ought to receive the credit.  But I'll only give out 100 points since the solution is just a workaround.
Thank you all for your help and your time.
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 6280082
Thanks.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 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

11 Experts available now in Live!

Get 1:1 Help Now