CFormView::OnInitialUpdate 
Author Message
 CFormView::OnInitialUpdate

Quote:
> >I'm trying to use a CFormView derived class in an SDI application. All of
> >the documentation and examples I've seen imply that OnInitialUpdate() is
> >called once, when the view is attached to the document, and thus can be
used
> >for one-time initialisation. However, I'm experiencing a call to
> >OnInitialUpdate() whenever a new document is opened. Is this the correct
> >behaviour?

> Dave,

> I would expect that behaviour.

> When you create a new document, I think the old document/view are
> destroyed and new ones created.

Thanks - I've thought about this, but I'm relatively sure that at least the
view is not destroyed - this is an SDI application. I became aware of this
when I try to initialise a CListCtrl in CFormView::OnInitialUpdate:

void CHVABar::OnInitialUpdate()
 {
    CFormView::OnInitialUpdate();
    SetScrollSizes(MM_TEXT,CSize(0,0));
    m_SectionList.InsertColumn(0,"Show",LVCFMT_CENTER,20);
    m_SectionList.InsertColumn(1,"ID",LVCFMT_RIGHT,20);
    m_SectionList.InsertColumn(2,"Name",LVCFMT_LEFT,70);
 }

I find that every time a document is opened, three additional columns are
inserted into the control. I'm completely new to MFC, so please bear with
me. If this is expected behaviour, is there another overridable/message I
should use for one off initialisation of the control, or am I expected to
keep an initialised flag?

Dave



Wed, 20 Aug 2003 18:37:48 GMT  
 CFormView::OnInitialUpdate
You can call CListCtrl::DeleteAllItems in your OnInitialUpdate and do
something such as the following.

void CListControl::DeleteAllColumns() {
    int i, n;
n = GetHeaderCtrl()->GetItemCount();
for (i=0; i<n; ++i)
    DeleteColumn(0);

Quote:
}



Quote:
> > >I'm trying to use a CFormView derived class in an SDI application. All
of
> > >the documentation and examples I've seen imply that OnInitialUpdate()
is
> > >called once, when the view is attached to the document, and thus can be
> used
> > >for one-time initialisation. However, I'm experiencing a call to
> > >OnInitialUpdate() whenever a new document is opened. Is this the
correct
> > >behaviour?

> > Dave,

> > I would expect that behaviour.

> > When you create a new document, I think the old document/view are
> > destroyed and new ones created.

> Thanks - I've thought about this, but I'm relatively sure that at least
the
> view is not destroyed - this is an SDI application. I became aware of this
> when I try to initialise a CListCtrl in CFormView::OnInitialUpdate:

> void CHVABar::OnInitialUpdate()
>  {
>     CFormView::OnInitialUpdate();
>     SetScrollSizes(MM_TEXT,CSize(0,0));
>     m_SectionList.InsertColumn(0,"Show",LVCFMT_CENTER,20);
>     m_SectionList.InsertColumn(1,"ID",LVCFMT_RIGHT,20);
>     m_SectionList.InsertColumn(2,"Name",LVCFMT_LEFT,70);
>  }

> I find that every time a document is opened, three additional columns are
> inserted into the control. I'm completely new to MFC, so please bear with
> me. If this is expected behaviour, is there another overridable/message I
> should use for one off initialisation of the control, or am I expected to
> keep an initialised flag?

> Dave



Wed, 20 Aug 2003 18:52:15 GMT  
 CFormView::OnInitialUpdate

Quote:
>Thanks - I've thought about this, but I'm relatively sure that at least the
>view is not destroyed - this is an SDI application.

You're quite right - I was mistaken. I have come across this myself
and had forgotten about it.

The view isn't deleted in an SDI application. In
CSingleDocTemplate::OpenDocumentFile, MFC re-uses the existing
document and view. OnInitialUpdate is called to get the view to update
with the new document data. I think that I ended up doing effectively
what Sam suggests, though an alternative may be to initialise your
list control columns in the WM_CREATE message (OnCreate).

Dave Lowndes
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Fri, 22 Aug 2003 00:55:54 GMT  
 CFormView::OnInitialUpdate


Quote:
> >Thanks - I've thought about this, but I'm relatively sure that at least
the
> >view is not destroyed - this is an SDI application.

> You're quite right - I was mistaken. I have come across this myself
> and had forgotten about it.

> The view isn't deleted in an SDI application. In
> CSingleDocTemplate::OpenDocumentFile, MFC re-uses the existing
> document and view. OnInitialUpdate is called to get the view to update
> with the new document data. I think that I ended up doing effectively
> what Sam suggests, though an alternative may be to initialise your
> list control columns in the WM_CREATE message (OnCreate).

Thanks - I appreciate the info. Unfortunately, the list control asserts in
SetImageList if called in OnCreate:

ASSERT(::IsWindow(m_hWnd));

I've worked round the problem now by using an initialisation flag in the
form view derived class.

Dave



Fri, 22 Aug 2003 01:23:23 GMT  
 CFormView::OnInitialUpdate

Quote:
>Thanks - I appreciate the info. Unfortunately, the list control asserts in
>SetImageList if called in OnCreate:

>ASSERT(::IsWindow(m_hWnd));

That'll be because the list control hasn't been created yet - which
raises the question of when do you create the list control?

If the flag works, then that's fine - I was just trying to identify a
possibly more elegant solution that wouldn't need it.

Dave Lowndes
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Fri, 22 Aug 2003 05:02:12 GMT  
 CFormView::OnInitialUpdate


Quote:
> >Thanks - I appreciate the info. Unfortunately, the list control asserts
in
> >SetImageList if called in OnCreate:

> >ASSERT(::IsWindow(m_hWnd));

> That'll be because the list control hasn't been created yet - which
> raises the question of when do you create the list control?

The list control is a ClassWizard generated member.

Quote:
> If the flag works, then that's fine - I was just trying to identify a
> possibly more elegant solution that wouldn't need it.

It works perfectly - I too would prefer an elegant solution, but testing a
single flag is preferable to destroying the controls' contents and
rebuilding it whenever a new document is opened.

Dave



Fri, 22 Aug 2003 05:48:06 GMT  
 CFormView::OnInitialUpdate

Quote:
>> That'll be because the list control hasn't been created yet - which
>> raises the question of when do you create the list control?

>The list control is a ClassWizard generated member.

I'd forgot we were initially talking about a CFormView!

In that case you may be able to do it by handling the WM_INITDIALOG
message and performing the 1-off initialisation there. You can't use
OnInitDialog for a CFormView - but handling the message does seem to
work in a superficial test I've tried:

Header file:

        afx_msg LRESULT OnInitForm(WPARAM, LPARAM);

Source file:

BEGIN_MESSAGE_MAP(CSdiformView, CFormView)
        //{{AFX_MSG_MAP(CSdiformView)
        //}}AFX_MSG_MAP
        ON_MESSAGE( WM_INITDIALOG, OnInitForm )
END_MESSAGE_MAP()

LRESULT CSdiformView::OnInitForm(WPARAM, LPARAM)
{
        // Whatever initialisation you need to do on the control
        // You may need to use GetDlgItem on the control rather
        // than use the member variable as the member variable
        // may not be attached at this point (I've not tested it)
        ...

        return Default();

Quote:
}

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.


Fri, 22 Aug 2003 06:22:32 GMT  
 CFormView::OnInitialUpdate
OnInitialUpdate replaces WM_INITDIALOG (and if you trace back far
enough, I think I recall is actually triggered by it). As pointed out,
you can't do anything at all in OnCreate; in fact, OnCreate is fairly
useless for CFormView (and CDialog). Part of the trick on reuse is to
consider having your OnDestroy handler delete and reset everything
that needs to be deleted, and OnInitialUpdate setting everything to
what it needs to be set to. This includes all
enabling/disabling/showing/hiding of controls. This is how I'd
approach the problem.

Also, as pointed out, OnInitialUpdate is intended to initialize the
view for a particular document, so in SDI it will be called
"repeatedley" for each new document that is opened; that is the
defined behavior.
                        joe



Quote:
>>> That'll be because the list control hasn't been created yet - which
>>> raises the question of when do you create the list control?

>>The list control is a ClassWizard generated member.

>I'd forgot we were initially talking about a CFormView!

>In that case you may be able to do it by handling the WM_INITDIALOG
>message and performing the 1-off initialisation there. You can't use
>OnInitDialog for a CFormView - but handling the message does seem to
>work in a superficial test I've tried:

>Header file:

>    afx_msg LRESULT OnInitForm(WPARAM, LPARAM);

>Source file:

>BEGIN_MESSAGE_MAP(CSdiformView, CFormView)
>    //{{AFX_MSG_MAP(CSdiformView)
>    //}}AFX_MSG_MAP
>    ON_MESSAGE( WM_INITDIALOG, OnInitForm )
>END_MESSAGE_MAP()

>LRESULT CSdiformView::OnInitForm(WPARAM, LPARAM)
>{
>    // Whatever initialisation you need to do on the control
>    // You may need to use GetDlgItem on the control rather
>    // than use the member variable as the member variable
>    // may not be attached at this point (I've not tested it)
>    ...

>    return Default();
>}

>Dave

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Thu, 28 Aug 2003 02:51:46 GMT  
 CFormView::OnInitialUpdate
See MS KB article "Q103982 - INFO: OnInitialUpdate() May Be Called More Than
Once". Yes, OnInitialUpdate in SDI will be called "repeatedley" for each new
document, but you can do something in OnCreate.

I might misunderstand what you are saying about OnInitialUpdate (the handler
of the MFC private message WM_INITIALUPDATE) and WM_INITDIALOG, but they are
both used by MFC. Only one is used, depending on whether the application is
using a view or a dialog. The WM_INITDIALOG message is sent by Windows and
the WM_INITIALUPDATE message is sent by MFC.



Quote:
> OnInitialUpdate replaces WM_INITDIALOG (and if you trace back far
> enough, I think I recall is actually triggered by it). As pointed out,
> you can't do anything at all in OnCreate; in fact, OnCreate is fairly
> useless for CFormView (and CDialog). Part of the trick on reuse is to
> consider having your OnDestroy handler delete and reset everything
> that needs to be deleted, and OnInitialUpdate setting everything to
> what it needs to be set to. This includes all
> enabling/disabling/showing/hiding of controls. This is how I'd
> approach the problem.

> Also, as pointed out, OnInitialUpdate is intended to initialize the
> view for a particular document, so in SDI it will be called
> "repeatedley" for each new document that is opened; that is the
> defined behavior.
> joe



> >>> That'll be because the list control hasn't been created yet - which
> >>> raises the question of when do you create the list control?

> >>The list control is a ClassWizard generated member.

> >I'd forgot we were initially talking about a CFormView!

> >In that case you may be able to do it by handling the WM_INITDIALOG
> >message and performing the 1-off initialisation there. You can't use
> >OnInitDialog for a CFormView - but handling the message does seem to
> >work in a superficial test I've tried:

> >Header file:

> > afx_msg LRESULT OnInitForm(WPARAM, LPARAM);

> >Source file:

> >BEGIN_MESSAGE_MAP(CSdiformView, CFormView)
> > //{{AFX_MSG_MAP(CSdiformView)
> > //}}AFX_MSG_MAP
> > ON_MESSAGE( WM_INITDIALOG, OnInitForm )
> >END_MESSAGE_MAP()

> >LRESULT CSdiformView::OnInitForm(WPARAM, LPARAM)
> >{
> > // Whatever initialisation you need to do on the control
> > // You may need to use GetDlgItem on the control rather
> > // than use the member variable as the member variable
> > // may not be attached at this point (I've not tested it)
> > ...

> > return Default();
> >}

> >Dave

> Joseph M. Newcomer [MVP]

> Web: http://www3.pgh.net/~newcomer
> MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm



Sun, 14 Sep 2003 11:45:15 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. CFormView::OnInitialUpdate

2. CFormView::OnInitialUpdate never gets called

3. CFormView within a CFormView error

4. CFormView on CFormView?

5. Closing a CFormView window from another CFormView window

6. Problems with CFormView inside CFormView (mnemonic keys)

7. Appwizard CFormView Assertion Error (also no or blank dialog (CFormView)) VS6.0 and VSNETBETA2

8. How to insert a CFormView in a CFormView?

9. Reusing database object twice in OnInitialUpdate

10. OnDraw before OnInitialUpdate

11. OnInitialUpdate from CView

12. Order of execution of OnInitialUpdate and Draw for multiple views

 

 
Powered by phpBB® Forum Software