Microsoft KB Archive/185671

= FIX: ClassWizard Uses DISPID_CAPTION as Custom Caption Property =

Article ID: 185671

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition 

-



This article was previously published under Q185671



SYMPTOMS
When loading an ActiveX control that has a custom "Get/Set methods" Caption property, into a Visual Basic form, Visual Basic won't persist the value of the Caption properly.



CAUSE
ClassWizard uses the DISPID for the stock Caption property (DISPID_CAPTION) as the DISPID for the custom Get/Set methods "Caption" property. When such a control is loaded to a form in Visual Basic and a save operation is performed, Visual Basic is confused so it saves the "Caption" property twice in the .frm file. When Visual Basic reads it in again, it encounters the first persisted property.



RESOLUTION
Give a unique DISPID to the custom "Get/Set methods" Caption property so that Visual Basic does not use the stock Caption property DISPID: <ol>  In the .odl file, change the following line (where the ID is equal to a nonconflicting DISPID). For example, change the following [id(DISPID_CAPTION)] BSTR Caption; to the following: [id(2)] BSTR Caption; </li>  Add an enumeration to the .h file of the COleControl-derived class: enum { //AFX_DISP_ID };                   </li>  Change the following line in the .cpp file of the COleControl-derived class. For example, change the following DISP_PROPERTY_EX_ID(CMyActiveXCtrl, "Caption", DISPID_CAPTION,        GetCaption, SetCaption, VT_BSTR) to the following: DISP_PROPERTY_EX(CMyActiveXCtrl, "Caption",        GetCaption, SetCaption, VT_BSTR) Change the following BoundPropertyChanged(DISPID_CAPTION); to the following: BoundPropertyChanged(dispidCaption); </li> Recompile the control project in Visual C++.</li> In the Visual Basic application, delete the control from the form. Save these changes to delete the multiple caption persisted properties. Then, add the control back to the form.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in Visual C++ version 6.0.

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Create an ActiveX control with MFC ActiveX ControlWizard.</li>  Add a custom "Get/Set methods" property named "Caption" with ClassWizard. The following is generated by ClassWizard: // The following lines are added to your control's .h file: afx_msg BSTR GetCaption; afx_msg void SetCaption(LPCTSTR lpszNewValue);

// The following line is added to dispatch map of your control's .cpp // file: DISP_PROPERTY_EX_ID(CMyActiveXCtrl, "Caption", DISPID_CAPTION,        GetCaption, SetCaption, VT_BSTR)

// The following methods are added to your control's .cpp file: BSTR CMyActiveXCtrl::GetCaption {        CString strResult; // TODO: Add your property handler here.

return strResult.AllocSysString; }

void CMyActiveXCtrl::SetCaption(LPCTSTR lpszNewValue) {        // TODO: Add your property handler here.

SetModifiedFlag; }

// The following line is added to your control's Object // Description (.ODL) file: [id(DISPID_CAPTION)] BSTR Caption; </li>  Add a member variable to your control's class named m_Caption of type CString. Then, make the following changes to the following functions in your control's .cpp file: void CMyActiveXCtrl::DoPropExchange(CPropExchange* pPX) {        ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor)); COleControl::DoPropExchange(pPX);

PX_String(pPX, _T("Caption"), m_Caption, _T("Caption")); }

BSTR CMyActiveXCtrl::GetCaption {        CString strResult; strResult=m_Caption; return strResult.AllocSysString; }

void CMyActiveXCtrl::SetCaption(LPCTSTR lpszNewValue) {        SetModifiedFlag; m_Caption=lpszNewValue;

BoundPropertyChanged(DISPID_CAPTION); }                   </li> Build the control's project and load the control to a form in Visual Basic.</li> Change the value of Caption property for the control from "Caption" to "Change Caption" in Visual Basic's Properties Window. Then, save this project.</li> Load the control's project in Visual Basic. The caption's value is "Change Caption." Now, change it from "Change Caption" to "More Change Caption". Save this project again.</li> Load the control's project once again in Visual Basic. However, the caption value is "Change Caption" instead of "More Change Caption."</li> If you open the .frm file of the control with Notepad, you will notice that there are two persisted Caption properties. One is "Change Caption" and the other is "More Change Caption."</li></ol>

<div class="references_section">