Jump to content
dormky

How do I close a modal form without ModalResult being set to mrCancel ?

Recommended Posts

1 hour ago, dormky said:

Then please explain ? I delphi, I write to a variable that is then monitored to close the form. In Window, I call a function. Those two things are completely different.

Imagine if you would read documentation. Like that for EndDialog. And imagine if the documentation for EndDialog said this:

 

 

 

EndDialog does not destroy the dialog box immediately. Instead, it sets a flag and allows the dialog box procedure to return control to the system. The system checks the flag before attempting to retrieve the next message from the application queue. If the flag is set, the system ends the message loop, destroys the dialog box, and uses the value in nResult as the return value from the function that created the dialog box.

 

 

 

So yeah, it's exactly the same as the Delphi mechanism. Because that's how modal dialog are implemented in Windows. 

  • Like 2

Share this post


Link to post
2 hours ago, dormky said:

There's a reason Delphi is a dead language

Not that again!

 

Just remove the f*ck*** call to Close and be done with it.

  • Haha 2

Share this post


Link to post
16 minutes ago, dummzeuch said:

Not that again!

 

Just remove the f*ck*** call to Close and be done with it.

But, the language is dead because he doesn't have to call Close. If he removes the call to Close and suddenly everything works the way it was designed to, then the language may come back to life. Sounds dangerous. 

Edited by Brandon Staggs

Share this post


Link to post
15 hours ago, David Heffernan said:

Imagine if you would read documentation. Like that for EndDialog. And imagine if the documentation for EndDialog said this:

 

 

 

EndDialog does not destroy the dialog box immediately. Instead, it sets a flag and allows the dialog box procedure to return control to the system. The system checks the flag before attempting to retrieve the next message from the application queue. If the flag is set, the system ends the message loop, destroys the dialog box, and uses the value in nResult as the return value from the function that created the dialog box.

 

 

 

So yeah, it's exactly the same as the Delphi mechanism. Because that's how modal dialog are implemented in Windows. 

Yes, but the difference is that the flag is internal. We do not have access to it, nor is it the same as the value returned by ShowModal call.

Again, to close in Windows I call function. In Delphi I set a flag. Fundamentally different things.

Share this post


Link to post
25 minutes ago, dormky said:

Again, to close in Windows I call function. In Delphi I set a flag. Fundamentally different things.

Yes, yes, we see now. How could we have been so blind? Totally different things. Totally!

Now, run along.

  • Thanks 1
  • Haha 1

Share this post


Link to post
2 hours ago, Anders Melander said:

Yes, yes, we see now. How could we have been so blind? Totally different things. Totally!

Instead of running away from this terrible problem. Anders you should write a class helper to help to solve this gigantic bug in the design. I mean how can we live and use Delphi knowing that it has this very poorly design in the fundamental part of the VCL.

Share this post


Link to post
Just now, Lajos Juhász said:

Instead of running away from this terrible problem. Anders you should write a class helper to help to solve this gigantic bug in the design. I mean how can we live and use Delphi knowing that it has this very poorly design in the fundamental part of the VCL.

I dunno. I think it's pointless. I mean, properties? Pfft! What's the point?

Delphi is dead anyway and if there's one thing the past 30 years has shown it is that the design is flawed beyond repair but we were just too ignorant to notice. If only we had been told about this before. The shame is unbearable.

  • Haha 1

Share this post


Link to post

Hey, bad design doesn't mean it's not useful. I still use a car whose AC is less powerful then a fart lol.

Share this post


Link to post

Luckily Delphi isn't the only IDE out there. Freedom of choice is a thing.

Share this post


Link to post
23 hours ago, dormky said:

Heavy disagree. A message/event driven environment should, you know, actually use events instead of mixing in state monitoring.

The VCL has to work in the API environment that Windows provides. So aim your criticism at Microsoft ;).

Share this post


Link to post
5 hours ago, dormky said:

Yes, but the difference is that the flag is internal. We do not have access to it, nor is it the same as the value returned by ShowModal call.

Again, to close in Windows I call function. In Delphi I set a flag. Fundamentally different things.

Windows API can't expose a flag directly so has to provide a function to set it. You are incapable of learning, no wonder this is so hard for you. 

Share this post


Link to post
24 minutes ago, David Heffernan said:

You are incapable of learning, no wonder this is so hard for you. 

OK, now we go into the domain of personal insults.

Share this post


Link to post

Yeah, I feel the question has been answered. Everybody should now move on...nothing to see here.

Share this post


Link to post
On 1/30/2025 at 5:20 AM, David Heffernan said:

Wow. This is so wrong. That call to Close is definitely getting executed. Setting ModalResult doesn't raise an exception. Which would be the only way to avoid Close getting called.

Setting ModalResult can be overridden, as well as Close, in some damned obscure ways. That's all I'm saying. 

 

But calling Close after setting ModalResult is completely unnecessary.

 

And if setting ModalResult isn't closing the form, then tossing a call to Close in there may or may not work, or may not do what you expect. 

 

This is based on my experience. YMMV.

Share this post


Link to post
2 hours ago, David Schwartz said:

And if setting ModalResult isn't closing the form, then tossing a call to Close in there may or may not work, or may not do what you expect. 

 

This is based on my experience. YMMV.

No, that call to Close should never be made. No ifs and buts. Stop guessing at how the system works. 

 

2 hours ago, David Schwartz said:

Setting ModalResult can be overridden

Really, how ? 

Share this post


Link to post
On 2/1/2025 at 12:02 PM, David Heffernan said:

No, that call to Close should never be made. No ifs and buts. Stop guessing at how the system works. 

 

Really, how ? 

By implementing your own ShowModal which doesn't rely on it and instead listens for a "close" event, I suppose. Maybe you could make a "Close" function which would raise this event and set the return value to the value given by argument... You know, just how EndDialog does it 😉

Share this post


Link to post
On 1/31/2025 at 12:35 PM, Sherlock said:

Luckily Delphi isn't the only IDE out there. Freedom of choice is a thing.

Of course, I never would have chosen Delphi for a new project. But there's a lot of old 90's projects out there for which it's not cost-effective to move to a more efficient platform, so Delphi sticks around.

Share this post


Link to post
24 minutes ago, Attila Kovacs said:

What would you have chosen instead?

I think a language where you can close a modal form without-modalresult being set to mrcancel..

  • Haha 1

Share this post


Link to post
1 minute ago, Die Holländer said:

I think a language where you can close a modal form without-modalresult being set to mrcancel..

You mean, like Delphi?

Share this post


Link to post
1 hour ago, dormky said:

By implementing your own ShowModal which doesn't rely on it and instead listens for a "close" event, I suppose. Maybe you could make a "Close" function which would raise this event and set the return value to the value given by argument... You know, just how EndDialog does it 😉

EndDialog does it exactly the same way as Delphi does it, we already established that. 

Share this post


Link to post
47 minutes ago, Attila Kovacs said:

What would you have chosen instead?

Depends on the project of course, and secondarily on my current skillset. For the project that spawned this thread, I probably would have gone with WPF, which would have given me easy access to the .NET ecosystem which would be useful since there's a lot of places with custom drawing, while the rest of the interface is very basic.

Share this post


Link to post
Just now, David Heffernan said:

EndDialog does it exactly the same way as Delphi does it, we already established that. 

Yes, and I established that it does it _internally_ and that you don't have a single flag serving two purposes, as you do in Delphi.

Share this post


Link to post

Delphi in order to survive must ASAP include the method to close a modalForm or we will die. As a Quick Fix they should include the class helper for all the existing versions:

 

type
  TFormHelper = class helper for TCustomForm
  public
    procedure EndDialog(PModulResult: TModalResult);
  end;

 

{ TFormHelper }

procedure TFormHelper.EndDialog(PModulResult: TModalResult);
begin
  modalResult:=PModulResult;
end;
 

I can only hope that somebody from Embarcadero will read this thread and will push these changes directly to the TForm not to require a class helper for such an important issue to be fixed.

 

(For years I was using the modalResult to close modal forms without a single issue. This thread made me realize that Delphi is dying because it does not have such a method.)

  • Haha 6

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×