?
Solved

Change from Modal to Modeless Dialog C++

Posted on 2005-04-01
8
Medium Priority
?
758 Views
Last Modified: 2008-01-09
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
Comment
Question by:corduroy9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:stu_pb
ID: 13681761
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13681776
you can use:
SaveTickler.Create(ID_TEMPLATE_OF_DIALOG);
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13681811
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
Industry Leaders: 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!

 
LVL 16

Expert Comment

by:imladris
ID: 13681911
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
 
LVL 2

Author Comment

by:corduroy9
ID: 13682053
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
 
LVL 16

Accepted Solution

by:
imladris earned 1050 total points
ID: 13682384
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13684180
>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
 
LVL 16

Expert Comment

by:imladris
ID: 13698161
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question