Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Recourse not found


This question is answered.


Permlink Replies: 4 - Last Post: Apr 3, 2018 12:25 AM Last Post By: Peter Below Threads: [ Previous | Next ]
Melissa Torn

Posts: 143
Registered: 4/30/09
Recourse not found  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 2, 2018 2:33 AM
Form2 is NOT auto create form
I am getting Resource not found.... error msg.
( if Form2 is auto create then i dont get error msg.)
what am i doing wrong ?

Thank you

type
TMyForm = Class(TForm);

procedure TForm1.ShowForm(Dataset:TDataset;MyForm: TMyForm);
begin
if MyForm=Nil then
MyForm:=TMyForm.Create(Application);
if Dataset <> Nil then
Dataset.Open;
MyForm.Show;
end;

procedure TForm1.TV1Click(Sender: TObject);
begin
if TTreeView(Sender).Selected.Text='Book List' then
ShowForm(Nil,TMyForm(Form2));
end;
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Recourse not found  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 2, 2018 3:43 AM   in response to: Melissa Torn in response to: Melissa Torn
Melissa Torn wrote:

Form2 is NOT auto create form
I am getting Resource not found.... error msg.
( if Form2 is auto create then i dont get error msg.)
what am i doing wrong ?

Everything <g>.

Thank you

type
TMyForm = Class(TForm);

There is no DFM file for this form class, but you try to create an
instance of it in your ShowForm method. That is where the code blows
up. When form2 is autocreated the variable you pass to ShowForm is not
nil, so the method does not try to create a TMyForm instance.


procedure TForm1.ShowForm(Dataset:TDataset;MyForm: TMyForm);
begin
if MyForm=Nil then
MyForm:=TMyForm.Create(Application);
if Dataset <> Nil then
Dataset.Open;
MyForm.Show;
end;

When you do not autocreate forms it is best to simply delete the form
variable the IDE creates for you, that prevents accidental misuse of
the variable. In your construction above the form created inside the
method would not be accessible via the form2 variable, by the way,
since you pass the variable by value and not by reference (var
parameter).

I'm not sure what you are trying to achieve here. If you want a method
that will show a form of a given form class and create it if it does
not already exist you have to construct the method like this:

Function ShowOrCreateFormByClass( aFormClass: TFormClass ): TForm;
Var
i: Integer;
Begin
For i:= 0 To Screen.formCount - 1 Do Begin
Result := Screen.Forms[i];
If Result.ClassType = aFormClass Then
Exit;
End;
Result := aFormClass.Create( Application );
Result.Show;
End;

procedure TForm1.TV1Click(Sender: TObject);
begin
if TTreeView(Sender).Selected.Text='Book List' then
TForm(form2) := ShowOrCreateFormByClass(TForm2);
end;

Note that you pass the form class, not the form variable, and since
the method returns the instance typed as TForm you need a typecast to
be able to store the instance into a variable typed as TForm2.

This scheme breaks down if you need to create more than one instance of
a given form class.


--
Peter Below
TeamB

Melissa Torn

Posts: 143
Registered: 4/30/09
Re: Recourse not found  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 2, 2018 5:35 AM   in response to: Peter Below in response to: Peter Below
Thank you so much Peter.
I need to show more than 1 forms (not auto created) at the same time
Is there a way to show all not auto created forms at the same time ?

What i'm trying to do is,
I have 12 forms some of them are auto created some not.
I can write a code for each process (lets say for every button click ) like below

procedure Button1.Click;
If Form1=Nil then
Form1:=TForm1.Create(Self);
Form1.Show;

procedure Button2.Click;
If Form2=Nil then
Form2:=TForm2.Create(Self);
Form2.Show;

procedure Button3.Click;
If Form3=Nil then
Form3:=TForm3.Create(Self);
Form3.Show;

so on...

but i dont want to repeat a form create for each process
i thought that i could write a code (function or proc) and use it
every button click.

Thank You
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Recourse not found
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2018 12:25 AM   in response to: Melissa Torn in response to: Melissa Torn
Melissa Torn wrote:

Thank you so much Peter.
I need to show more than 1 forms (not auto created) at the same time
Is there a way to show all not auto created forms at the same time ?

procedure TForm1.CreateOrShow(var aFormVar: TForm; aFormClass:
TFormClass);
begin
if not Assigned(aFormVar) then
aFormVar := aFormclass.Create(self);
aFormVar.Show;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
CreateOrShow(TForm(form2), TForm2);
end;


What i'm trying to do is,
I have 12 forms some of them are auto created some not.
I can write a code for each process (lets say for every button click
) like below

procedure Button1.Click;
If Form1=Nil then
Form1:=TForm1.Create(Self);
Form1.Show;

procedure Button2.Click;
If Form2=Nil then
Form2:=TForm2.Create(Self);
Form2.Show;

procedure Button3.Click;
If Form3=Nil then
Form3:=TForm3.Create(Self);
Form3.Show;

so on...

but i dont want to repeat a form create for each process
i thought that i could write a code (function or proc) and use it
every button click.

Thank You


--
Peter Below
TeamB

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Recourse not found  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 2, 2018 10:12 AM   in response to: Peter Below in response to: Peter Below
Peter Below wrote:

There is no DFM file for this form class, but you try to create an
instance of it in your ShowForm method.

This is what the resource error is complaining about. This has nothing
to do with auto-create vs manual-create. Make sure your Form class has
a .dfm file with the same base filename as the source file (ie,
MyForm.dfm if the Form's source file is MyForm.pas), and a {$R *.dfm}
directive.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02