| | | | Browse by category |
Problem
How can I create a subclassed combo box where the dropdown list items are determined at runtime?
Cause
Action
You have several choices:
- Take a look at the article "How can I fill the choice list of a combo box depending on a value in another cell?". This lets you supply the choice list at runtime.
- You can derive a class from CGXComboBoxWnd and implement an owner drawn combo box. See the article "How can I implement an owner-drawn combo box?"
- 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 |
||||
};
// 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 if (IsActive( )) |
||||
GetWindowText(s); | ||||
else | ||||
s = style.GetValueRef( ); | ||||
// Select item lbox->SetCurSel(atoi(s)); // or |
||||
}
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( ); |
||||
} | ||||
} |