Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IW "FormName" is already created?
#1
Hi guys its me again i have problem with with user with more than 2 have error showing form is already created. how to deal with this kind of logic i tried release and create method like i asked before its not use 

thank you
Reply
#2
Each session can only have one form with a particular name. Thus, you can't have 2 instances named 'IWForm1' in any session.

First thing you have to ask yourself is:
- Why do you need 2 instances of the same form in a session? 
- Can't you just use the same instance over and over, instead of creating a new one? or...
- Can't you destroy the old instance before creating a new one?
- Why do you need to set a particular name to that form? (in general, the name of the form is irrelevant).

TIWApplication has several methods to help you to manage your forms:


Code:
function TIWApplication.FindFormByName(const AFormName: string): TComponent;
function TIWApplication.FindFormByClassName(const AFormClassName: string): TComponent;


You can use something like this:

Code:
function TIWForm1.GetOrCreateForm(AFormClass: TIWAppFormClass): TIWAppForm;
var
 LForm: TComponent;
begin
 LForm := WebApplication.FindFormByClassName(AFormClass.ClassName);
 if not Assigned(LForm) then begin
   LForm := AFormClass.Create(WebApplication);
 end;
 Result := LForm as TIWAppForm;
end;

I use a function like the one above to create my forms in my IW applications, and I use it like this:

Code:
procedure TIWForm2.Button1Click(Sender: TObject);
var
 LForm: TIWAppForm;
begin
 LForm := GetOrCreateForm(TIWForm1);
 LForm.Show;
end;

As you can see, I just need the class of the form, I don't need to specify or deal with the name of the form instance. IW will do that for you automatically, unless you have a good reason to do so.
The function GetOrCreateForm() creates a new form of the class TIWForm1 if one doesn't exist. Otherwise it will return the existing instance.
When you don't need this instance anymore, all you need to do is call its Release() method.

So, let's say that

Code:
procedure TIWForm2.Button1Click(Sender: TObject);
var
 LForm: TIWAppForm;
begin
  Self.Release;                         // release current form TIWForm2
 LForm := GetOrCreateForm(TIWForm1);   // creates a new instance of TIWForm1 (or returns an existing instance)
 LForm.Show;                           // makes this new instance the active form
end;

Also note that IWForm2.Release can be safely called before creating a new form of class TIWForm1 (and making it the active form). Release just marks the form for destruction, it doesn't destroy it immediately.
Reply
#3
(09-28-2018, 10:17 AM)Alexandre Machado Wrote: Each session can only have one form with a particular name. Thus, you can't have 2 instances named 'IWForm1' in any session.

First thing you have to ask yourself is:
- Why do you need 2 instances of the same form in a session? 
- Can't you just use the same instance over and over, instead of creating a new one? or...
- Can't you destroy the old instance before creating a new one?
- Why do you need to set a particular name to that form? (in general, the name of the form is irrelevant).

TIWApplication has several methods to help you to manage your forms:


Code:
function TIWApplication.FindFormByName(const AFormName: string): TComponent;
function TIWApplication.FindFormByClassName(const AFormClassName: string): TComponent;


You can use something like this:

Code:
function TIWForm1.GetOrCreateForm(AFormClass: TIWAppFormClass): TIWAppForm;
var
 LForm: TComponent;
begin
 LForm := WebApplication.FindFormByClassName(AFormClass.ClassName);
 if not Assigned(LForm) then begin
   LForm := AFormClass.Create(WebApplication);
 end;
 Result := LForm as TIWAppForm;
end;

I use a function like the one above to create my forms in my IW applications, and I use it like this:

Code:
procedure TIWForm2.Button1Click(Sender: TObject);
var
 LForm: TIWAppForm;
begin
 LForm := GetOrCreateForm(TIWForm1);
 LForm.Show;
end;

As you can see, I just need the class of the form, I don't need to specify or deal with the name of the form instance.  IW will do that for you automatically, unless you have a good reason to do so.
The function GetOrCreateForm() creates a new form of the class TIWForm1 if one doesn't exist. Otherwise it will return the existing instance.
When you don't need this instance anymore, all you need to do is call its Release() method.

So, let's say that

Code:
procedure TIWForm2.Button1Click(Sender: TObject);
var
 LForm: TIWAppForm;
begin
 Self.Release;                         // release current form TIWForm2
 LForm := GetOrCreateForm(TIWForm1);   // creates a new instance of TIWForm1 (or returns an existing instance)
 LForm.Show;                           // makes this new instance the active form
end;

Also note that IWForm2.Release can be safely called before creating a new form of class TIWForm1 (and making it the active form). Release just marks the form for destruction, it doesn't destroy it immediately.

Hi Alexandre thank you for you reply. I usually do front-end, back-end and , database but delphi is whole new level to me i thought delphi iwform can surf anywhere like you do in html, php but it doesn't thank you for your guidance

Code:
TIWAppForm(WebApplication.ActiveForm).Release;
TuserAddForm.Create(WebApplication).Show();
i change form like this usually but this is no good anymore i think
Reply
#4
Your code should work correctly.
Do you have more details when the error happens? Are you setting form name inside some event in TuserAddForm class?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)