FIX: ClassWizard Uses DISPID_CAPTION as Custom Caption Property
ID: Q185671
|
The information in this article applies to:
-
The Microsoft Foundation Classes (MFC), included with:
-
Microsoft Visual C++, 32-bit Editions, version 5.0
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:
- 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;
- Add an enumeration to the .h file of the COleControl-derived class:
enum {
//{{AFX_DISP_ID(CPersistctrlCtrl)
// ... more dispid enumeration goes here
dispidCaption = 2L // ADD THIS!!! - same DISPID as in step 1
//}}AFX_DISP_ID
};
- 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);
- Recompile the control project in Visual C++.
- 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.
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.
MORE INFORMATIONSteps to Reproduce Behavior
- Create an ActiveX control with MFC ActiveX ControlWizard.
- 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;
- 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);
}
- Build the control's project and load the control to a form in Visual
Basic.
- Change the value of Caption property for the control from "Caption" to
"Change Caption" in Visual Basic's Properties Window. Then, save this
project.
- 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.
- Load the control's project once again in Visual Basic. However, the
caption value is "Change Caption" instead of "More Change Caption."
- 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."
REFERENCES
For a list of Stock Properties Supported by ClassWizard, please refer to
"ActiveX Controls: Adding Stock Properties" in Visual Basic's books online.
© Microsoft Corporation 1998, All Rights Reserved. Contributions by Yeong-Kah Tam, Microsoft Corporation
Additional query words:
Keywords : kbwizard kbMFC kbVC kbVC500bug kbVC600fix
Version : winnt:5.0
Platform : winnt
Issue type : kbbug
|