It's not an error to dynamically create a control. (Unless you didn't
where dynamic creation is appropriate. Showing/hiding controls has
its' place too, and more often than not it more convenient to do this.
But there's definitely no error in adding a control at runtime.
Joseph M. Newcomer wrote:
It's simple, really. There is a difference between a C++ object and
a kernel object.
Declaring a C++ object declares a C++ object; it does not create the
kernel object that
goes along with it. There are many reasons you would want to
decouple these, such as
needing a variable that represents an existing object.
If it created the button, you would need a separate constructor, that
included parameters
such as the control ID, control rectangle, and parent window.
Therefore, you would not
write
CButton button;
you would write
CButton button(rect, id, this);
or
CButton * button = new CButton(rect, id this);
Note that there are many reasons you want to create pointers to
objects, but be aware that
it is a common beginner's error to think that you have to use 'new'
everywhere, such as
dynamically creating a button. Dynamically creating a button is a
separare concept from
dynamically creating a window, and it is not at all uncommon to have
a structure that
contains a CButton, and you create the structure first (such as a
CDialog-derived class)
and only later create the button object and bind it to the variable.
In addition, there
is another common error: if you want a control to be optional, there
is a belief that you
only create it if you need it. Instead, the typical practice is to
create it at design
time, mark it as invisible/disabled in either the dialog editor or
OnInitDialog, and do a
ShowWindow(SW_SHOW)/EnableWindow(TRUE) when you need it.
So what is it you are actually trying to accomplish?
joe
****
On Mon, 5 Jan 2009 10:11:43 -0800 (PST), Mishra <mt.vijay@gmail.com>
wrote:
Hi All,
I have one question which is not regarding any compilation or logical
issue. It is related to MFC.
In below code,
CButton *pbutton = new CButton; // Just to create object;
Pbutton->create(.) // to create actual button control.
Anyone has idea that what the purpose of having separate Create(.)
function in MFC?
I mean, we could have different CButton Constructor which would
initialize and create Button control also. Then why we have different
function "Create"
Button control can be created using CButton constructor if MFC would
have allowed us.
Thanks,
Mishra
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm