Desperate - How to close wxDialog?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Desperate - How to close wxDialog?

Post by doublemax »

Wanderer82 wrote: Mon Jul 19, 2021 10:17 pm Well, for what reason should I evaluate the return value of ShowModal()? The user has to close the dialog himself. In the dialog he can put products into the basket and when he's finished, he closes the dialog window.
But what if he wants to cancel the action and do nothing? Usually you would have a Cancel and OK/Apply button and call EndModal(wxID_OK) only when the user hits the OK button. In all other cases (Cancel button, Window close icon, or pressing ESC), you'd call EndModal(wxID_CANCEL). The value you pass to EndModal() will be the return value of the ShowModal() call.
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Desperate - How to close wxDialog?

Post by Wanderer82 »

First of all... I have different groups of products. The dialog we are talking about here, shows the products for one group. In this window I put the "Apply" button for each product that goes inside the basket. If the user wants to take one or all of them out, he has to Cancel it for each product. If he wants to choose more from another group, he closes the window by just clicking on the cross in the top right.

But I'm not yet at the end of my design here, it's just how it works so far. Normally, if you put different items in a basket, you can't cancel the basket in the same dialog or webpage where you put the product into the basket. You go to the basket first and then empty it or delete individual items. But I'll keep this in mind if I need another functionality.

But when we're talking about EndModal(). Until now I used Close(). So I'll replace all Close() wirh EndModal(). I think that's the right way to do. But I wonder: In my dialog I have no special button to close it. And in the deconstructor - which I guess is called when the user clicks the cross - there is no EndModal(). Why is that? Is EndModal() called anyway when the deconstructor is called?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Desperate - How to close wxDialog?

Post by ONEEYEMAN »

Hi,
Is the dialog have 2 buttons or just one?

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Desperate - How to close wxDialog?

Post by doublemax »

Wanderer82 wrote: Mon Jul 19, 2021 10:44 pm And in the deconstructor - which I guess is called when the user clicks the cross - there is no EndModal(). Why is that? Is EndModal() called anyway when the deconstructor is called?
EndModal() just exits the modal loop and gives control back to the calling code (= returns from ShowModal()). Destroying the dialog is a different issue. If the dialog was created on the stack, destruction happens when it goes out of scope.
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Desperate - How to close wxDialog?

Post by Wanderer82 »

ONEEYEMAN wrote: Mon Jul 19, 2021 11:53 pm Hi,
Is the dialog have 2 buttons or just one?

Thank you.
The dialog has a button for each product (to put it in the basket). There are no other / general buttons to close the window or apply changes etc. Only the standard "cross" to close the window is there.
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Desperate - How to close wxDialog?

Post by Wanderer82 »

doublemax wrote: Tue Jul 20, 2021 5:06 am
Wanderer82 wrote: Mon Jul 19, 2021 10:44 pm And in the deconstructor - which I guess is called when the user clicks the cross - there is no EndModal(). Why is that? Is EndModal() called anyway when the deconstructor is called?
EndModal() just exits the modal loop and gives control back to the calling code (= returns from ShowModal()). Destroying the dialog is a different issue. If the dialog was created on the stack, destruction happens when it goes out of scope.
So, calling EndModal() means that the dialog goes out of scope, right?
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: Desperate - How to close wxDialog?

Post by doublemax@work »

So, calling EndModal() means that the dialog goes out of scope, right?
No. "Going out of scope" means something different. A local variable goes "out of scope" when the code block in which it was defined, ends. At that time the variable will be destroyed.

Code: Select all

void foo()
{
   if( condition == true )
   {
       wxString s = "hello";
   } 
  // can't access variable "s" here. It's "out of scope" and already destroyed.
}
Post Reply