Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6 C++ C MFC
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 22-Jan-12 5:28am
tomay30001.2K
Edited 22-Jan-12 5:32am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
tomay3000 at 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 at 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.
tomay3000 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

At least, there are two possible entries Smile | :) :
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()
};
  Permalink  
Comments
tomay3000 at 23-Jan-12 11:12am
   
Thats it ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
tomay3000 at 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_» at 22-Jan-12 20:55pm
   
You would need to subclass the edit control if you need to do this.
Look at SubclassDlgItem.
tomay3000 at 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
tomay3000 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 23 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100