3

According to this link from Microsoft, it should be possible to define a CButton and specify its parent window (CDialog) without having the CButton as a member of the Dialog, but I couldn't do it.

So if myButton is a member of a CDialog-derived (myCDialog) class, the following code works:

BOOL myCDialog::OnInitDialog() {
  CDialog::OnInitDialog();
myButton.Create(_T("My button"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(10, 20, 100, 50), this, 1000); 
...
}

But when I talk about dynamic creation, I want to be able to create as many buttons as I want dynamically (I can't define them as class members, because I don't know how many!)

I have tried the following code in another class with a pointer to myCDialog as the parent window, similar to the code shown in the link, but it fails:

CButton myButton;
myButton.Create(_T("My button"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(10, 20, 100, 50), pmyCDialog, 1000);

So, how can I create dynamic controls without defining them as a member of the CDialog class?

towi_parallelism
  • 1,421
  • 1
  • 16
  • 38
  • What about https://msdn.microsoft.com/en-us/library/6zxw3b4a.aspx – Jerry Jeremiah Jun 11 '15 at 10:48
  • What do you mean by "it fails"? Can you be more specific? – rrirower Jun 11 '15 at 11:49
  • @JerryJeremiah: In that example, still a member is used. `m_edit` is defined as a member of the CDialog-derived class – towi_parallelism Jun 11 '15 at 12:04
  • @rrirower: sure! I get a n "assertion failure". says from wincore.cpp Line 790! Debug Assertion Failed! which is this line: `ASSERT(pParentWnd != NULL);` – towi_parallelism Jun 11 '15 at 12:05
  • 1
    You need to make sure the pointer to the parent window is not null. Have you stepped through the code and watched the value of that pointer when you try to create the control? – rrirower Jun 11 '15 at 12:27
  • @rrirower: Uh! I had done a stupid thing. It was in the constructor of the other class. the pointer wasn't ready at that time, but still I don't see the button. It doesn't fail. But the button is not visible. I use the same code. When myButton is a member of the class, and `this` is used, it works, but now that I declare it as an `extern CButton myButton;` and define it in another class; create it with pointer to the CDialog, nothing is shown. – towi_parallelism Jun 11 '15 at 12:51
  • 1
    When all else fails, step through the code (including MFC) and make sure pointers, CRects, etc. are all valid. Check return codes if provided. – rrirower Jun 11 '15 at 12:53

1 Answers1

1

"(I can't define them as class members, because I don't know how many!)"

You can make an array, or a vector, of CButton or CButton* as a class member. Assign a different ID to each of them when you call its Create.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
  • Well, actually, this is not a complete answer. The reason why I couldn't make it work was that the reference was lost. It has to be valid through the entire program. The solution I came up with was to use dynamic allocation for buttons. @ScottMcP-MVP: please update your answer accordingly, such that other users can benefit from it. – towi_parallelism Jul 27 '15 at 15:02
  • You can**not** make an array, or a vector, of CButton. (CButton* is possible, though). Because all the subclasses of CObject, such as CButton, are unable to copy-construct or assign(=). See [this](https://stackoverflow.com/a/74302557/10027592). – starriet Nov 04 '22 at 07:05