1

After the previous question with issue in inserting a Rich Edit control to MFC Dialog based application I am here with another question:

I am using Visual Studio 2015 So I am creating a Dialog based MFC application:

If I add a Rich Edit control from the Toolbox it works fine But: How to create it programmatically?

In InitInstance I added:

AfxInitRichEdit2();

And in button "Press" handler l added:

CRichEditCtrl edt;
RECT rct{ 10, 10, 200, 200 };
edt.Create(WS_VISIBLE | WS_CHILD | ES_MULTILINE, rct, this, 150);

But when running the application No Rich edit there?!!

Maestro
  • 2,512
  • 9
  • 24
  • 4
    Well, did you press the button to create the RichEdit? Also, when `edt` goes out of scope, it will destroy the RichEdit, so you should make that variable be a member of your dialog class instead. – Remy Lebeau Nov 08 '18 at 20:29
  • @RemyLebeau Yes for sure. How should I do it? – Maestro Nov 08 '18 at 20:30
  • 3
    Move the declaration of `edt` into the declaration of your dialog class (what `this` refers to), in its `public`, `protected`, or `private` section, your choice. Do you not know how to work with classes in general? – Remy Lebeau Nov 08 '18 at 20:32
  • @RemyLebeau: Oh thank you!!! I changed it to dynamic and it works. `CrichEditCtrl* edt = new CRichEditCtr...`. Now I should make it a global scope for example. – Maestro Nov 08 '18 at 20:33
  • 3
    Well, now you have a memory leak, because now you need to call `delete edt;` when you are done using the RichEdit, but you can't do that because `edt` is a local variable and you lose access to it. Again, you need to make `edt` be a member of your dialog class instead – Remy Lebeau Nov 08 '18 at 20:34
  • @RemyLebeau: It works thank you. the problem really it is on the stack and it gets out of scope thus its ctor is called. as a result no edit there. – Maestro Nov 08 '18 at 20:35
  • 3
    That is only a problem when `edt` is a local variable of the procedure that declares it, not when it is a member of your dialog class – Remy Lebeau Nov 08 '18 at 20:36
  • @RemyLebeau: I got what you really wanted to say. I know there's a memory leak. Ok I declare it as a member variable. So this issue concern each type of control? doesn't it? Remy is it good to create it on the stack or dynamically allocated? – Maestro Nov 08 '18 at 21:54
  • 3
    If you allocate it dynamically, you have to keep track of it so you can destroy it later when you are done using it. If it is a member of the dialog class, there is no need to allocate it dynamically, it will go out of scope when the dialog object is destroyed. – Remy Lebeau Nov 08 '18 at 22:16
  • @RemyLebeau: I'll always appreciate you. You are always helpful and informative. – Maestro Nov 08 '18 at 22:17
  • @RemyLebeau One last question: If i created the control with Wizard does it mean that the wizard created it as a member variable? – Maestro Nov 08 '18 at 22:20
  • @RemyLebeau: After declaring the control as a member variable of the dialog: if i press the button "PRESS" I get the dialog there and everything is ok. But if re-press the button I get assertion dialog. I figured out by : `if(!m_richEdit) m_richEdit.Create(...)` so it is created once. Does this correct? – Maestro Nov 08 '18 at 22:29
  • 2
    Yes, the wizard creates controls as members. And yes, you would have to check that the RichEdit window hasn't already been created before creating a new one. – Remy Lebeau Nov 08 '18 at 23:12
  • @RemyLebeau: Thanks again for the thorough, useful explanation. – Maestro Nov 09 '18 at 14:06

0 Answers0