Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Dynamic Forms without Release



Permlink Replies: 5 - Last Post: Jan 14, 2015 5:17 AM Last Post By: Alexandre Machado
Drew Whitehead

Posts: 3
Registered: 6/19/03
Dynamic Forms without Release
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2015 1:55 PM
Hello,

Under XII, I used to have the following code in the "NewSession" event
handler:

ASession.Data := TIWUserSession.Create(nil);
UserSession.WebRedirect := TWebRedirect.Create(ASession);
UserSession.WebOrderFormA := TWebOrderFormA.Create(ASession);
UserSession.WebOrderFormB := TWebOrderFormB.Create(ASession);
UserSession.WebOrderFormC := TWebOrderFormC.Create(ASession);
UserSession.WebOrderFormD := TWebOrderFormD.Create(ASession);
VMainForm:=UserSession.WebOrderFormA;

Now under XIV, I see that I have to move these procedures into
"GetMainForm" event handler. However, I no longer have access to
ASession. I can change that to WebApplication like I've seen in some
of my examples.

Now, here is my problem, how do I create all four web forms and switch
between them without creating and release them each time they appear?
In all of the demo apps, I see creating the new form and releasing the
old one.

How do I get the handle to each one of my forms so I can just show and
hide them?

Thanks!

Drew
Daniel Fields

Posts: 622
Registered: 11/29/04
Re: Dynamic Forms without Release
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2015 5:09 PM   in response to: Drew Whitehead in response to: Drew Whitehead
Unless you transposed an error during your post, it appears that you are accessing UserSession when it should be ASession.Data. ASession.Data is the local variable created in that event. It is an instance of the UserSession.

ASession.Data := TIWUserSession.Create(nil);
ASession.Data.WebRedirect := TWebRedirect.Create(ASession);
ASession.Data.WebOrderFormA := TWebOrderFormA.Create(ASession);
ASession.Data.WebOrderFormB := TWebOrderFormB.Create(ASession);    
ASession.Data.WebOrderFormC := TWebOrderFormC.Create(ASession);
ASession.Data.WebOrderFormD := TWebOrderFormD.Create(ASession);     
VMainForm:=ASession.Data.WebOrderFormA;


Edited by: Daniel Fields on Jan 13, 2015 5:59 PM Corrected a mistake. It should have said ASession.Data instead of UserSession
Drew Whitehead

Posts: 3
Registered: 6/19/03
Re: Dynamic Forms without Release [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2015 7:44 PM   in response to: Daniel Fields in response to: Daniel Fields
Thanks Dan. However, in the GetMainForm procedure as changed in XIV,
I don't have access to ASession?

procedure TIWServerController.IWServerControllerBaseGetMainForm(
var vMainForm: TIWBaseForm);

I'm trying to create the forms in that procedure as documented here:
http://docs.atozed.com/docs.dll/development/Migrating%20to%20IntraWeb%20XIV.html

As documented in the "dynamically setting the main form" section.

Any thoughts on this?


On Tue, 13 Jan 2015 18:01:11 -0800, Daniel Fields <> wrote:

Unless you transposed an error during your post, it appears that you are accessing UserSession when it should be ASession.Data. ASession.Data is the local variable created in that event. It is an instance of the UserSession.

ASession.Data := TIWUserSession.Create(nil);
ASession.Data.WebRedirect := TWebRedirect.Create(ASession);
ASession.Data.WebOrderFormA := TWebOrderFormA.Create(ASession);
ASession.Data.WebOrderFormB := TWebOrderFormB.Create(ASession);    
ASession.Data.WebOrderFormC := TWebOrderFormC.Create(ASession);
ASession.Data.WebOrderFormD := TWebOrderFormD.Create(ASession);     
VMainForm:=ASession.Data.WebOrderFormA;


Edited by: Daniel Fields on Jan 13, 2015 5:59 PM Corrected a mistake. It should have said ASession.Data instead of UserSession
Daniel Fields

Posts: 622
Registered: 11/29/04
Re: Dynamic Forms without Release [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2015 11:18 PM   in response to: Drew Whitehead in response to: Drew Whitehead
I am transitioning to XIV, but have not converted that part of my application. I would assume that UserSession would be the correct reference in that event. It seems that something as simple as the snippet below should work. I would assume that event fires once for each session.

begin
  UserSession.WebRedirect := TWebDirect.Create(???); //WebApplication or self as the owner?
  vMainForm := UserSession.WebRedirect;
end;


I think I commented on a similar issue a couple of weeks ago. The topic there was setting the form a the start of the session. That person had something special that had to be handled outside the Server Controller as well.

The solution was to have the main form of your application function as a splash screen. You could display a simple loading message to begin with and add graphics later. You would put a timer on that form and use its Async event to fire your code. You can set the interval to a second or less. You could then use the timer event to create any forms or variables you need. The last step of that event would be to use WebApplication.SetActiveForm(UserSession.MyForm) to redirect to any page you like. That would allow you to access the form variables in your user session in the proper context.
Daniel Fields

Posts: 622
Registered: 11/29/04
Re: Dynamic Forms without Release
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2015 5:40 PM   in response to: Drew Whitehead in response to: Drew Whitehead
When you are changing forms you can test for its existence and create it conditionally.

if WebApplication.FindComponent('IWForm1') <> nil then
  WebApplication.SetActiveForm(WebApplication.FindComponent('IWForm1') as TIWContainer)
else
begin
  AForm := TIWForm1.Create(WebApplication);
  WebApplication.SetActiveForm(AForm);
end;


For your specific example you could use something like this

if UserSession.WebRedirect = nil then
  UserSession.WebRedirect := TWebRedirect.Create(WebApplication);
WebApplication.SetActiveForm(UserSession.WebRedirect);


Edited by: Daniel Fields on Jan 13, 2015 7:10 PM
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: Dynamic Forms without Release
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 14, 2015 5:14 AM   in response to: Drew Whitehead in response to: Drew Whitehead
You can use some code like that:


var
  myFormInstance: TWebOrderFormA;
begin
  myFormInstance :=  WebApplication.FindFormByClassName('TWebOrderFormA');
  if not Assigned(myFormInstance) then begin
    myFormInstance := TWebOrderFormA.Create(WebApplication);
  end;
  myFormInstance.Show;
end;
 


There is also another WebApplication method, FindFormByName(), that can be used when you have 2 different form instances of the same class, but different names.

or can create a more generic code to handle that. Check this UserSessionUnit code:

unit UserSessionUnit;
 
interface
 
uses
  IWUserSessionBase, SysUtils, Classes, IWAppForm;
 
type
  TIWUserSession = class(TIWUserSessionBase)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ShowForm(AFormClass: TIWAppFormClass);
  end;
 
implementation
 
{$R *.dfm}
 
{ TIWUserSession }
 
procedure TIWUserSession.ShowForm(AFormClass: TIWAppFormClass);
var
  myFormInstance: TComponent;
begin
  myFormInstance :=  WebApplication.FindFormByClassName(AFormClass.ClassName);
  if not Assigned(myFormInstance) then begin
    myFormInstance := AFormClass.Create(WebApplication);
  end;
  TIWAppForm(myFormInstance).Show;
end;
 


Also, make sure that your ServerController.OnNewSession event is following the "new" pattern:


procedure TIWServerController.IWServerControllerBaseNewSession(
  ASession: TIWApplication);
begin
  ASession.Data := TIWUserSession.Create(nil, ASession);
end;
 


In this case, the ASession (TIWApplication instance) is passed as the second parameter to the IWUserSession constructor. This way, it can be used within the IWUserSession.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02