Click to See Complete Forum and Search --> : Removing a dynamic control


July 29th, 1999, 10:11 AM
Hello,

I'm dynamically create an ActiveX control by using the CreateControl call but I can't find a way to delete the control once I no longer want it. Is there anything in MFC that allows you to remove a Active X control?

August 1st, 1999, 12:08 PM
CreateControl() can return a CWnd object ( i think ), check the class library for the required function, i think it is CWnd::CloseWindow() or CWnd::DestroyWindow(), sorry don't Dev studio on this machine

ric
August 2nd, 1999, 06:01 AM
Destoying controls by yourself can be very risky causing in the best case memory leaks. If you use CWnd::DestroyWindow(), as suggested, this will call only the base class destructor and guess what will happen.

My suggestion:

Make it a stack based object and it will be destructed either when the function exits or when your program enters the CWinApp::OnIdle() function.

Duncan Forsyth
November 24th, 1999, 06:36 AM
I'm _so_ hoping that you're still following this thread. I've been trying to find someone who has, successfully, dynamically created ActiveX controls, for the past few days.

I'd like to dynamically create an ActiveX masked edit control within a VC++
application.

I've generated the source for CMSMask, and added it to the project.

The dialog in which this all happens is created by a DLL. The DLLs CWinApp
derived class has an InitInstance() method which calls
AfxEnableControlContainer().

I've made a call to the CMSMask constructor and called Create() upon it. [ I've tried CreateControl with the first argument (the class name) as "MaskEdBox" and as "MSMaskWndClass" but both caused CreateControl to return false. ]

I set the font (successfully) and then call SetMaxLength(). This results in
an exception being thrown which causes the message box "Invalid Property
Value" to be displayed. I get the same effect if I call ModifyStyleEx().

The call to Create also causes exceptions to be thrown. The exceptions are
caught before Create returns. They are all DISP_E_MEMBERNOTFOUND.


I suspect that I have failed to call some OLE initialisation method.

Care to set me on the right track?