Accessing a form method by another form

Forums for specific tips, techniques and example code
basti
Posts: 13
Joined: Tue Oct 13, 2009 10:39 am

Accessing a form method by another form

Postby basti » Wed Oct 14, 2009 12:39 pm

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!

ascribe
Posts: 16
Joined: Mon Sep 14, 2009 1:51 pm
Location: Australia
Contact:

Re: Accessing a form method by another form

Postby ascribe » Wed Oct 14, 2009 2:59 pm

not sure if this is what you want


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

basti
Posts: 13
Joined: Tue Oct 13, 2009 10:39 am

Re: Accessing a form method by another form

Postby basti » Wed Oct 14, 2009 3:48 pm

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.

basti
Posts: 13
Joined: Tue Oct 13, 2009 10:39 am

Re: Accessing a form method by another form

Postby basti » Sun Oct 18, 2009 1:45 pm

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: ?

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Accessing a form method by another form

Postby murray » Sun Oct 18, 2009 9:16 pm

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'.
Murray (N.Z.)

basti
Posts: 13
Joined: Tue Oct 13, 2009 10:39 am

Re: Accessing a form method by another form

Postby basti » Sun Oct 18, 2009 10:45 pm

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" :(

User avatar
Dino
Posts: 49
Joined: Wed Jun 24, 2009 6:55 am
Location: London
Contact:

Re: Accessing a form method by another form

Postby Dino » Mon Oct 19, 2009 4:12 am

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.

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Accessing a form method by another form

Postby BeeJay » Mon Oct 19, 2009 9:22 am

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.

basti
Posts: 13
Joined: Tue Oct 13, 2009 10:39 am

Re: Accessing a form method by another form

Postby basti » Mon Oct 19, 2009 10:46 am


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!


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 11 guests