Page 1 of 1

Accessing a form method by another form

Posted: Wed Oct 14, 2009 12:39 pm
by basti
Hi there!

I think its a very simple problem but I just don´t find the solution.

I would like to access a method (showButton) in an already opened form (mainMenu) by another open form (showCustomers). How do I do the link in the showCustomers-form to the mainMenu-Form (which is a mdiFrame and showCustomers is a child)?

Something like:

Code: Select all

btnClose_click(btn: Button input) updating; //btnClose is part of the showCustomer-form vars begin mainMenu.showButton; self.unloadForm; end;
is not working - it doesn´t know the method showButton.

I think I need to specify the already opened mainMenu-form. But how?

Thanks in advance!

Re: Accessing a form method by another form

Posted: Wed Oct 14, 2009 2:59 pm
by ascribe
not sure if this is what you want


vars
frm : Form;
begin
frm := app.getForm("mainMenu").Form;
if frm <> null then
frm.showButton;
endif;

Re: Accessing a form method by another form

Posted: Wed Oct 14, 2009 3:48 pm
by basti
not sure if this is what you want


vars
frm : Form;
begin
frm := app.getForm("mainMenu").Form;
if frm <> null then
frm.showButton;
endif;
Thanks, it helped (again :) ). Although I wasn´t able to access the showButton-method. I don´t know why. Though, I can access all the basic methods like .load, .unload and so on. I just put the showButton-code in the forms .load method and thats working fine.

Re: Accessing a form method by another form

Posted: Sun Oct 18, 2009 1:45 pm
by basti
Now, I need to access a form by another form and cannot do it via the load-method.

Here is the problem: when I do this to look what type of user is logged in...

Code: Select all

load() updating; vars f : Form; begin f := app.getForm("HelpForm").Form; if f.loggedUserType="manager" then // <-- property "loggedUserType" is unknown self.groupAssign.visible:=true; endif; end;
... I always get the error: unknown property or method. The HelpForm however does have a property named loggedUserType of type string. And I also access the correct HelpForm, because I can unload the form with f.unloadForm - so that is working but I´m just not able to access/read the property.

What am I doing wrong :oops: ?

Re: Accessing a form method by another form

Posted: Sun Oct 18, 2009 9:16 pm
by murray
You are specifying a type guard of 'Form' which does not have that property defined.
Jade will only use properties visible to the class specified in the type guard clause.
Your call 'app.getForm' is requesting a specific subclass of Form with added properties.
Since f is declared as a Form, Jade will take it on face value that it is nothing else.
Try changing the variable type and/or using a type guard of '.HelpForm' instead of '.Form'.

Re: Accessing a form method by another form

Posted: Sun Oct 18, 2009 10:45 pm
by basti

Code: Select all

load() updating; vars f : HelpForm; begin f := app.getForm("HelpForm").HelpForm; if f.loggedUserType="manager" then // <-- property "loggedUserType" is unknown self.groupAssign.visible:=true; endif; end;
--> I tried this. Compiles without error but get an error during runtime:

Error: Attempted access via null object reference
Source: if f.loggedUserType="manager" :(

Re: Accessing a form method by another form

Posted: Mon Oct 19, 2009 4:12 am
by Dino
That means that f is null, so you're getting the exception at runtime because you're trying to access the loggedUserType property on a null object reference. The app.getForm method returns null if it cannot find an active form with the specified name, so the most likely cause is that your HelpForm isn't open. You can guard against the exception by checking that f isn't null, as follows.

Code: Select all

f := app.getForm("HelpForm").HelpForm; if f <> null then if f.loggedUserType="manager" then self.groupAssign.visible:=true; endif; else // do whatever you need to do when HelpForm isn't open end;
Dean.

Re: Accessing a form method by another form

Posted: Mon Oct 19, 2009 9:22 am
by BeeJay
when I do this to look what type of user is logged in...
One question that springs to mind is why you're looking at a property on your "Help" form to see what type of user is logged on and therefore adjust the visibility of your "assign" group on the current form?

This code is making an assumption that the user always has an open "Help" form at the time when this logic is going to run. A better approach would be to have a myCurrentUser reference on your application subclass and use this reference to determine your type of user. Assuming you had a method on your user class called say "isManager" then your quasi-code would look something like the following:

Code: Select all

self.groupAssign.visible := app.myCurrentUser.isManager ;
Note that you will of course need to setup the myCurrentUser reference on your application subclass during your initial application logon processing or you'll get a 1090 in the above code.

Cheers,
BeeJay.

Re: Accessing a form method by another form

Posted: Mon Oct 19, 2009 10:46 am
by basti

This code is making an assumption that the user always has an open "Help" form at the time when this logic is going to run. A better approach would be to have a myCurrentUser reference on your application subclass and use this reference to determine your type of user. Assuming you had a method on your user class called say "isManager" then your quasi-code would look something like the following:

Code: Select all

self.groupAssign.visible := app.myCurrentUser.isManager ;
Note that you will of course need to setup the myCurrentUser reference on your application subclass during your initial application logon processing or you'll get a 1090 in the above code.

Cheers,
BeeJay.
Oh boy, what a stupid mistake. Shame on me! ... Of course there is no HelpForm when a manager is logged in. ... Damn you brain - what were you thinking (were it thinking at all?). ... Yeah, I just added a loggedUser-property (to the main-menu form) which is then set up adequately during login process.

Many thanks!