Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

basic mfc help

Howdy,

I've just started dabbling in MFC after a few years of c++.  I had a short introduction to MFC book lying around so i thought i'd give it a shot on a project for one of my friends.  So far i think i've been doing pretty good using that book along with the MS site, but i've ran into a few problems and i'd kind of like to know some answers.

1.  What is the alternative for DoModal() ?  I mean, how can i make a child/popup window that allows the user to access the parent window?

2.  How do i make things load automatically when a popup Window is created?  For instance i want a name to be loaded into a text box when one of my dialog classes is instantiated.  If i put that code in the constructor it blows up on me because the DoModal() function hasn't been called.  I always have to have a button saying something similar to "show results", and when its clicked it loads the values on the screen.

If you want to see some of my code let me know, but more or less i just want to know some general ideas.

Thanks,
--Jason

0
solman66
Asked:
solman66
  • 2
1 Solution
 
AlexFMCommented:
1) Alternative of DoModal is modeless dialog. See CDialog::Create function. Notice that if you use modeless dialog, it is better to keep dialog instance as member of parent dialog class, because modeless dialog exists when function which creates it exits.

void CParentDialog::CreateUnmodalChild()
{
    m_Child.Create(...);  
}


2) Initialize dialog controls in OnInitDialog function. When parent initializws child dialog, it may set it's members which are not linked to controls. In OnInitDialog these members are used to set control values.

void CParentDialog::ShowModalChild()
{
     CChildDlg dlg(this);
     dlg.m_sString = "...";     // m_sString is CString
     dlg.DoModal();              // in CChildDlg::OnInitDialog use m_sString to show in some control
}
0
 
Karl Heinz KremerCommented:
The reason your software "blows up" when you try to initialize the text box is that the control is not yet displayed on the screen, and the routine you use (probably SetWindowText()) expects the control to be already created and displayed. Use AlexFM's suggestion to first set a member variable in your dialog class. By just setting this variable you are not making any assumptions about what's already displayed. Once OnInitDialog() is called, you know that the controls are created and you can safely use any methods to initalize them.
0
 
solman66Author Commented:
Thanks!

That worked perfectly, i have a few other questions that i want to try to work out myself, but if i have any other ones i can't find easily i know where to come.

I was pretty sure that was the reason the program would blow up, but its always niced to be re-assured.

Now i can go back through and re-write alot of my code.

One day last week, I had tried the "create" function to create a modeless dialog, but i don't think i was doing it right, i guess my only question about it is to make sure i understand m_Child correctly.  I'm taking that as an object of my child class, declared as a private member variable in the parent class.  If you don't get back to me on that its no big deal i'll probably test it out here later tonight.

Thanks again!
0
 
AlexFMCommented:
>> Object of my child class, declared as a private member variable in the parent class.

Right.
0

Featured Post

Technology Partners: 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!

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