Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 10 total
Thread MessageDlg Event Handler
Mon, Sep 21 2015 1:43 PMPermanent Link

squiffy

Telemix Ltd.

Avatar

I want to create a MessageDlg event handler (used for MsgDlgResult) that can be used by many MessageDlg calls across many forms, in an attempt to remove duplicate code.

If I define my MsgDlgResult procedure within the form class everything is fine, but if I try to define it in a common module it doesn't like it. I get this error :

[Error] UnitClientList.wbs (93,10): There is no function or procedure declaration that matches the MessageDlg('Please press the OK button
(or refresh your browser) to
log back in.', 'NOT LOGGED IN', mtError, , mbOk, refreshApp(), true) reference

I assume that is because it doesn't recognise refreshApp (the handler defined in a common unit).

I seem to be able to call other functions and the like in this common unit from this and other forms so I'm sure what I'm doing is broadly correct (in terms of using the unit and declaring the procedure public), but how do I get MessageDlg to use an "external" procedure in this case?

Hope that makes sense.
Mon, Sep 21 2015 2:20 PMPermanent Link

squiffy

Telemix Ltd.

Avatar

Think I've worked it out, but would appreciate someone a bit better than me passing opinion ...

In my type declarations at the start of the form, I have this :

TmyProc = procedure(DlgResult: TModalResult);

then further down in some method, I have :

var mp: TmyProc;
begin
   mp:=refreshApp;  // This is the proc defined in another unit.
   ...

I then use mp as the callback in the MessageDlg function.
Mon, Sep 21 2015 2:40 PMPermanent Link

squiffy

Telemix Ltd.

Avatar

No, doesn't work. The app just crashes and restarts. No obvious error in the browser.

Still googling...
Mon, Sep 21 2015 2:41 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com


<< If I define my MsgDlgResult procedure within the form class everything is fine, but if I try to define it in a common module it doesn't like it. I get this error : >>

Two things:

1) You can't pass a method reference with calling parentheses.  It needs to be passed like this:

MessageDlg('Please press the OK button (or refresh your browser) to log back in.', 'NOT LOGGED IN', mtError, , mbOk, refreshApp, true) reference

Notice that I removed the parentheses.

2) How is refreshApp defined ?  Is it a method (and not just a procedure) ?

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Sep 21 2015 2:43 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

<< No, doesn't work. The app just crashes and restarts. No obvious error in the browser. >>

That's because you're trying to pass a *procedure* reference to something that expects a *method* reference.  A procedure reference is missing a key piece: the instance on which the method should be called.  That's why it blows up.

Actually, the compiler should prevent you from doing this, but I have to do some checks to see why it isn't.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Sep 21 2015 2:50 PMPermanent Link

squiffy

Telemix Ltd.

Avatar

>> Notice that I removed the parentheses.
The actual code didn't have those - only the error message in the IDE when compiling. Here's the original code :

MessageDlg( 'Please press the OK button'
                    +#13+#10+'(or refresh your browser) to'
                    +#13+#10+'log back in.','NOT LOGGED IN',
                    mtError,[mbOk],mbOk,refreshApp,true);


>> How is refreshApp defined?
It is just a procedure and not a method.

I'm clearly overcomplicating it. I suppose the thing to do is just create the method in the form and call the procedure from there.
Mon, Sep 21 2015 6:03 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

<< The actual code didn't have those - only the error message in the IDE when compiling. >>

Ahh, okay.  I'll make sure that is corrected.

<< It is just a procedure and not a method. >>

Yes, that's your problem.  The MessageDlg parameter is an event (method) parameter, so it needs to be passed a method, and not just a procedure.

<< I'm clearly overcomplicating it. I suppose the thing to do is just create the method in the form and call the procedure from there. >>

It doesn't have to be a method of the form, but it has to be the method of *some* class.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Sep 22 2015 4:36 AMPermanent Link

squiffy

Telemix Ltd.

Avatar

>> It doesn't have to be a method of the form, but it has to be the method of *some* class.

Ahhh, I get it now. Ok, I created a skeleton class like this :

type TmyClass = class(TObject)
  public
     procedure SillyTest(DlgResult: TModalResult);
end;

Then defined a var like this :

procedure .....
var
  myRef: TmyClass;
begin             
  myRef:=TmyClass.Create;

The called it from the MessageDlg like this :

MessageDlg( 'Please press the OK button'
                    +#13+#10+'(or refresh your browser) to'
                    +#13+#10+'log back in.','NOT LOGGED IN',
                    mtError,[mbOk],mbOk,myRef.SillyTest,true);

And it seems to work just fine. Would you mind just confirming what I've done there is correct and I'm not storing up an issue for myself? I still think it's probably overkill for this particular situation, but if nothing else I'm getting a better handle on how everything works.

Thanks, Tim. Your help is, as always, greatly appreciated.
Tue, Sep 22 2015 4:44 AMPermanent Link

Matthew Jones

I'd use a method on your main form - and then just reference that each
time.

--

Matthew Jones
Tue, Sep 22 2015 11:00 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com


<< And it seems to work just fine. Would you mind just confirming what I've done there is correct and I'm not storing up an issue for myself? >>

Yes, that's correct.

Tim Young
Elevate Software
www.elevatesoft.com
Image