Solved

basic mfc help

Posted on 2004-04-06
4
289 Views
Last Modified: 2013-11-20
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
Comment
Question by:solman66
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 10771853
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
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10774201
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
 
LVL 2

Author Comment

by:solman66
ID: 10779002
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 10780765
>> Object of my child class, declared as a private member variable in the parent class.

Right.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now