Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to initialize a CEdit control with some text using SetWindowText function. The best plase to do that is After its creation & Before it is displayed to the screen.

I have chosen WM_CREATE message handler (OnCreate) to handle this initialization, & unfortunately no chance, this message handler has never been invoked!

I need some help here ;(
Posted
Updated 22-Jan-12 4:32am
v2

For template created controls, OnCreate() is never called, because the controls get created before the CWnd wrappers and are attached by DoDataExchange().

As already noted by Maximilien, the usual places to initialize controls are OnInitDialog() for dialogs and OnInitialUpdate() for views.

If you need a self-initializing control, you may use PreTranslateMessage() of the control, subclassing the control (see comment from _Superman_), or dynamically create controls.
 
Share this answer
 
Comments
Mr. Tomay 23-Jan-12 5:59am    
Do you mean PreSubclassWindow ?
Because I have seen this: http://www.codeguru.com/forum/showthread.php?t=67343
Jochen Arndt 23-Jan-12 6:42am    
PreSubclassWindow can be used to initialize controls (e.g. commonly used to modfiy styles). But it is only called once the conrol is subclassed (and never when the control is not subclassed). If you only want to set an initial value, using SetWindowText() from OnInitDialog() or OnInitialUpdate() is the usual way.
Mr. Tomay 23-Jan-12 11:07am    
I forgot to say that my edit control is subclassed (sorry)

class CNumericEdit : public CEdit
{
.
.
.
}


& I have found this one:
www.codeproject.com/Articles/480/Create-your-own-controls-the-art-of-subclassing
At least, there are two possible entries :) :
C++
class CBothContextableEdit : public CEdit
{
  //...

protected:
  virtual void PreSubclassWindow() {
    CEdit::PreSubclassWindow();
    SetWindowText(_T("test"));
  }
  
  afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct) {
    int iResult(CEdit::OnCreate(lpCreateStruct));
    if (GetSafeHwnd()) {
      SetWindowText(_T("test"));
    }
    return iResult;
  }

  DECLARE_MESSAGE_MAP()
};
 
Share this answer
 
Comments
Mr. Tomay 23-Jan-12 11:12am    
Thats it ;)
If the edit control is in a dialog, initialization must be done in the OnInitDialog method.

If the edit window is in a view (for example in the case of the CFormView), I think (it's been a while), you can do it in the OnInitialUpdate method.

M.
 
Share this answer
 
Comments
Mr. Tomay 22-Jan-12 15:56pm    
It is a CRecordView view.
I know that doing it inside OnInitialUpdate parent's message handler function solves the issue, but I am wondering if it could be done in the child edit control with some message handler without going the parent's one.
«_Superman_» 22-Jan-12 20:55pm    
You would need to subclass the edit control if you need to do this.
Look at SubclassDlgItem.
Mr. Tomay 23-Jan-12 11:09am    
I forgot to say that my edit control is subclassed (sorry)

class CNumericEdit : public CEdit
{
.
.
.
}


& I have found this one:
www.codeproject.com/Articles/480/Create-your-own-controls-the-art-of-subclassing
Mr. Tomay 23-Jan-12 6:04am    
Do you mean PreSubclassWindow ?
Because I have seen this: http://www.codeguru.com/forum/showthread.php?t=67343
Albert Holguin 23-Jan-12 11:13am    
I don't know who downvoted this but this solution is absolutely correct (probably someone who doesn't know MFC)... +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900