Home RAP

Create Declaration in Code

edited March 2003 in RAP
OK. I have managed to create the Event handler now but for some reason it
does not seem associated with the control that I would expect it to be
associated with.
I am creating an onGetMemo handler for a memo control and although the
control appears green in the RAP tab the OnGetMemo handler icon underlying
this appears as if no handler is assigned, and upon clicking it this is
indeed the case.

Upon changing to Module view the item appears under 'Event Handlers'
It appears as procedure IDBMEMO4OnGetMemo; (note the missing brackets and
Tstrings collection, though these are declared in the method itself).

The code is as follows:-


CodeMod:=TraCodeModule.CreateForReport(Report);
EventProg:= CodeMod.CreateEventHandler(Mem,szRoutineName);

if EventProg=nil then
begin
EventProg:=TraProgram.Create();
EventProg.ProgramName:=szFullRoutineName;
EventProg.ChildType:=ctCreateProgram;
EventProg.ProgramType:=ttProcedure;
end;

Populate the Method body here...

CodeMod.DSExclude([pppcEmpty]);

{ Compile the Code }

CodeMod.BuildAll(false);

I have not posted this beneath my other mail, even though it partially
answers it, as I have some questions still outstanding and I would quite
like them answering too.

Can any bright spark see where I am going wrong, any help would be
greatfully appreciated.

--
Regards

Conrad Rowlands
Callards Technology Ltd

Comments

  • edited March 2003
    I forgot to mention a few things in the first post, so I'll respond here.

    This is what you are looking for:


    {get the codemodule for the report}
    lCodeModule := raGetCodeModule(ppReport1);

    if (lCodeModule = nil) then
    begin
    lCodeModule := TraCodeModule.Create(ppReport1);
    lCodeModule.Report := ppReport1;
    end;

    In the tip project links provided previously, we make the call to pull the
    code module out in a button click event handler, because the form has been
    instantiated and there is a RAP code module object in existance in the
    report. You can also see that in some cases, such as in the the tip which
    moves the group header band before print event from one group to another we
    perform the above code. I have to say that the MoveGroupHeaderBeforePrint
    tip is cool- Another customer needed to swap event handlers because they
    shipped a report template with a bug in it as the RAP event handler was
    hooked up to the wrong group header band. The end user had already modified
    the template. So, to preserve the end user's template and fix the bug they
    didn't have the RAP IDE as an end user, I wrote an example that modified the
    RAP code behind the scenes to swap the event handlers for two group header
    bands and the end user was able to keep using his report.

    There is one example that is more important to you than the others- creating
    an event handler. In our main RAP demos, we show how to do this:

    {---------------------------------------------------------------------------
    ---}
    {TForm1.CreateRAPCode}

    procedure Trap0042Form.CreateRAPCode;
    var
    lCodeModule: TraCodeModule;
    lEventHandler: TraEventHandler;
    begin

    {get the codemodule for the report}
    lCodeModule := raGetCodeModule(FReport);

    {CreateEventHandler: parameters: Component, EventName
    - calling this method will create a RAP event-handler and
    generate a program shell containing the program declaration plus begin
    end}

    lEventHandler := lCodeModule.CreateEventHandler(FReport.DetailBand,
    'BeforeGenerate');

    {set BodyText to modify the code between the begin..end}
    lEventHandler.SourceLines.Text := lEventHandler.Declaration +
    'var' + #13#10 +
    ' lsFirstName: String;' + #13#10 +
    ' lsLastName: String;' + #13#10 +
    'begin' + #13#10 +
    ' lsFirstName :=
    plClient[''FIRST_NAME''];' + #13#10 +
    ' lsLastName :=
    plClient[''LAST_NAME''];' + #13#10 +
    ' LabelFullName.Caption := lsFirstName
    + '' '' + lsLastName;' + #13#10 +
    'end;';

    {compile the code}
    lCodeModule.BuildAll(True);

    {add code to the memo so we can print it}
    FMemoRAPCode.Text := lEventHandler.SourceLines.Text;

    end; {procedure, CreateRAPCode}

    Cheers,

    Jim Bennett
    Digital Metaphors


  • edited March 2003
    Thanks Jim, unfortunately I do not feel that the main thrust of my question
    is answered. Let us compare the code and I think you will see what I mean

    JB- lCodeModule := raGetCodeModule(FReport);
    CR- CodeMod:=TraCodeModule.CreateForReport(Report);

    JB- lEventHandler :=
    lCodeModule.CreateEventHandler(FReport.DetailBand,'BeforeGenerate');
    CR- EventProg:= CodeMod.CreateEventHandler(Mem,szRoutineName);

    CR - Also Had extraneous code here, that was never called and did not
    impact, now removed

    JB- lEventHandler.SourceLines.Text := etc...
    CR- Use the Add methods instead, should have no impact.

    CR- CodeMod.DSExclude([pppcEmpty]); (Not sure why this is neccessary but it
    was in a previous DM sample

    JB- lCodeModule.BuildAll(True);
    CR- CodeMod.BuildAll(false); (We do not wish for the op to fail on the part
    of a dodgy function)

    After running the designer I have the function in my designer and it appears
    that it is assigned to the control due to the GREEN arrows against the
    control, however upon selecting the appropriate method the code window does
    not show the appropriate code, it behaves as if the control contains no
    handlers.

    Upon inserting a handler into that control manually and then viewing the
    event handlers via the module view the declarations appear thus:-

    procedure IDBMemo2OnGetMemo; - My Handler
    procedure IDBMemo2OnGetMemo(Lines: Tstrings); - RAP Created Handler


    The thrust of my problem is that I wish my handler to be properly assigned.
    What, if anything am I doing wrong. Is the Compile without exceptions having
    an impact on this.

    Looking forward to your response.
  • edited March 2003
    Sorry if I was short on the examination of your code and didn't answer your
    question. There are small pieces which can make a difference when creating
    code at runtime that is going to be interpreted at runtime. I first wanted
    to give you some examples which worked for us here and was hoping to lean
    our your skills as a developer to compare and contrast the demos to your
    code. So, the difference is that you compile false and we compile true. What
    happens when you compile true? Does it work?

    If that doesn't work, then send me a very simple example project, where
    there is a report with a memo and a single RAP event handler that you want
    to create and populate the memo contents in the new event handler when it
    prints. I'll make the simple demo work and send it back to you. Send it to
    support@digital-metaphors.com

    Cheers,

    Jim Bennett
    Digital Metaphors


This discussion has been closed.