Call center available M-F 9:00 - 6:00 US Eastern Time.
U.S. and Canada
(877) 723-1442
International
(517) 625-5729
Email
Add controls inheritance to your application
If you need to add new functionality to standard Command Bar controls (CXTPControlButton, CXTPControlPopup, CXTPControlComboBox, CXTPControlEdit) you must create a new class inherited from one of these and override some virtual methods. This tutorial describes how to create custom Combo Box.
Imagine that you need a combo box with uppercase letters only (When users enter a character it automatically becomes uppercase)
- Create a new class inherited from CXTPControlComboBox
- To create custom edit we must override CreateEditControl method and add a new Edit Control inherited from CXTPControlComboBoxEditCtrl
- Add OnChar handler to automatically uppercase the characters that the user entered
- If your application uses customization you must add methods to restore your combo box after the program reloads
- Add standard DECLARE and IMPLEMENT macros
- If your control has its own variables that need to be stored/restored you must override Copy and Serialize methods
- Now you can create your control in the OnCreateControl handler
class CControlComboBoxEx: public CXTPControlComboBox
{
public:
};
class CControlComboBoxEx: public CXTPControlComboBox
{
public:
CXTPControlComboBoxEditCtrl* CreateEditControl()
{
return new CControlComboBoxEditEx();
}
};
class CControlComboBoxEditEx : public CXTPControlComboBoxEditCtrl
{
};
class CControlComboBoxEditEx : public CXTPControlComboBoxEditCtrl
{
public:
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CControlComboBoxEditEx,
CXTPControlComboBoxEditCtrl)
ON_WM_CHAR()
END_MESSAGE_MAP()
void CControlComboBoxEditEx::OnChar(UINT nChar,
UINT nRepCnt, UINT nFlags)
{
CString strChar((TCHAR)nChar), strUpper((TCHAR)nChar);
strUpper.MakeUpper();
if (strChar != strUpper) ReplaceSel(strUpper, TRUE);
else CXTPControlComboBoxEditCtrl::OnChar(nChar,
nRepCnt, nFlags);
}
class CControlComboBoxEx: public CXTPControlComboBox
{
DECLARE_XTP_CONTROL(CControlComboBoxEx)
public:
CXTPControlComboBoxEditCtrl* CreateEditControl()
{
return new CControlComboBoxEditEx();
}
};
IMPLEMENT_XTP_CONTROL(CControlComboBoxEx, CXTPControlComboBox)
class CControlComboBoxEx: public CXTPControlComboBox
{
…
protected:
void Copy(CXTPControl* pControl,
BOOL bRecursive = FALSE);
virtual void Serialize(CArchive& ar);
protected:
CString m_strMask;
};
void CControlComboBoxEx::Copy(
CXTPControl* pControl, BOOL bRecursive)
{
ASSERT_KINDOF(CControlComboBoxEx, pControl);
m_strMask = ((CControlComboBoxEx*)pControl)->m_strMask;
CXTPControlComboBox::Copy(pControl, bRecursive);
}
void CControlComboBoxEx::Serialize(CArchive& ar)
{
CXTPControlComboBox::Serialize(ar);
if (ar.IsStoring())
{
ar << m_strMask;
}
else
{
ar >> m_strMask;
}
}
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
ON_XTP_CREATECONTROL()
END_MESSAGE_MAP()
int CMainFrame::OnCreateControl(
LPCREATECONTROLSTRUCT lpCreateControl)
{
if (lpCreateControl->nID == ID_POPUP_COMBO)
{
CXTPControlComboBox* pComboFind =
(CXTPControlComboBox*)CControlComboBoxEx::CreateObject();
pComboFind->SetDropDownListStyle();
pComboFind->SetFlags(xtpFlagManualUpdate);
lpCreateControl->pControl = pComboFind;
return TRUE;
}
return FALSE;
}