How can I create a subclassed combobox where the dropdown list items are determined at runtime?
You have several choices:
a) Take a look at the FAQ "How can I fill the choice list of a combobox depending on a value in another cell?". This lets you supply the choice list at runtime.
b) You can derive a class from CGXComboBoxWnd and implement an owner drawn combobox. See the FAQ "How can I implement an owner-drawn combobox?".
c) You might subclass a control from CGXComboBox as in the following example:
Example:
// see CGridSample5View (gridsvw5.cpp) for info on how
// to register subclassed controls.
//
// CMyComboBox is a sample class
class CMyComboBox: public CGXComboBox
{
DECLARE_CONTROL(CMyComboBox);
public:
// Constructor & Destructor
CMyComboBox(CGXGridCore* pGrid, UINT nEditID, UINT nListBoxID, UINT nFlags);
~CMyComboBox( );
// Override these methods if you don't want to fill the
// list from the choice-list
virtual void OnStoreDroppedList(CListBox* lbox);
virtual void OnFillDroppedList(CListBox* lbox);
};
// CMyComboBox
CMyComboBox::CMyComboBox(CGXGridCore* pGrid, UINT nEditID, UINT nListBoxID, UINT nFlags)
: CGXComboBox(pGrid, nEditID, nListBoxID, nFlags)
{
m_bFillWithChoiceList = FALSE;
}
CMyComboBox::~CMyComboBox( )
{
}
void CMyComboBox::OnFillDroppedList(CListBox* lbox)
{
const CGXStyle& style = Grid( )->LookupStyleRowCol(m_nRow, m_nCol);
// fill with Choices at runtime
{
while (...)
{
CString sItem;
...
lbox->AddString(sItem);
}
}
// Set Window Text and select item
CString s;
if (IsActive( ))
GetWindowText(s);
else
s = style.GetValueRef( );
// Select item
lbox->SetCurSel(atoi(s));
// or
lbox->SelectString(-1, s);
}
void CMyComboBox::OnStoreDroppedList(CListBox* lbox)
{
// Save the selection
int index = lbox->GetCurSel( );
if (index != LB_ERR)
{
CString s;
char sz[20];
lbox->GetText(index, s);
SetActive(TRUE);
SetWindowText(s);
SetSel(0,-1);
SetModify(TRUE);
Invalidate( );
}
}