• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 762
  • Last Modified:

Change from Modal to Modeless Dialog C++

Hi,

I have a DLL which opens a Modal Window except now I realize this would be better as a modeless.  If I have

      CSaveTicklerDialog SaveTickler;
      SaveTickler.DoModal();

What am I going to have to change.

As much detail possible would be appreciated.
0
corduroy9
Asked:
corduroy9
1 Solution
 
stu_pbCommented:
I believe there is a Dialog.DoModeless() function.  So your code would be:

 CSaveTicklerDialog SaveTickler;
 SaveTickler.DoModeless();

I think that may be the only change necessary.

Good Luck!
0
 
Jaime OlivaresSoftware ArchitectCommented:
you can use:
SaveTickler.Create(ID_TEMPLATE_OF_DIALOG);
0
 
Jaime OlivaresSoftware ArchitectCommented:
After using, you have to be aware of this:

SomeFunction()
{
CSaveTicklerDialog SaveTickler;
SaveTickler.Create(ID_TEMPLATE_OF_DIALOG);
}

In such case dialog will not appear because it will be created and inmediatly destroyed, because SaveTicker object gets out of scope. Typical implementation is:

// Define somewhere outside the function: CSaveTicklerDialog  *pSaverTicker;

SomeFunction()
{
pSaveTickler->Create(ID_TEMPLATE_OF_DIALOG);
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
imladrisCommented:
Yes, Modeless dialogs require a lot more care than modal ones. There are a lot of little things to know.

Here's a helpful link:

http://www.codeproject.com/dialog/gettingmodeless.asp

(And no, there is no such thing, in windows or MFC, as DoModeless)
0
 
corduroy9Author Commented:
jamie i like your approach on this but I dont understand putting the create outside of the function.  Why is this needed?  Why does it get destroyed immediately.  I need it to go through all the normal steps I have in my Modal dialog like OnInitDialog, and OnOk... etc...
0
 
imladrisCommented:
The problem with this code:

SomeFunction()
{
CSaveTicklerDialog SaveTickler;
SaveTickler.Create(ID_TEMPLATE_OF_DIALOG);
}


Is that the first line ("CSaveTicklerDialog SaveTickler") creates the dialog object on the stack (analogous to what you would get if you declared "int i"). Anything declared on the stack will disappear when the function returns.

Now, in a modal dialog, creating it on the stack is usually ok. It works because the function that calls DoModal is not going to exit/return until the dialog completes; it is only when the dialog compeletes that the DoModal returns and execution carries on in that function.

But in the modeless case, the Create call returns immediately, *leaving* the dialog up and working. So when the function that called Create returns a dialog object that is on the stack will go "out of scope", which means its destructor will be called, and so the dialog will disappear.

One simple way to avoid this problem is to create the dialog on the heap, instead, with a call to new.
Then whereever you call create from will *not* cause the dialog object to go out of scope, and so it won't disappear.
0
 
Jaime OlivaresSoftware ArchitectCommented:
>Why does it get destroyed immediately?
Because when function terminates, the object gets out of scope.
If you declare an integer variable inside this function, the variable is destroyed when function ends, it is the same case.

The dialog can be created inside the function, but the variable (pointer) must be declared outside the function, could be a global variable, or maybe as a member of other object like the Application object.
Also I recently noticed I have included a constructor call, so I will rewrite the function:

SomeFunction()    // some global function, or some object's member function
{
// pSaveTickler is defined somewhere outside
   if (!pSaveTickler) {  // check to not create twice
        pSaveTickler = new CSaveTicklerDialog;
        pSaveTickler->Create(ID_TEMPLATE_OF_DIALOG);
   }
}
0
 
imladrisCommented:
Did any of that help you?

If so, it is now time to select an answer and grade it.

If not, perhaps a clarifying question would further the process.....
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now