CImageList / CComboBoxEx - what's wrong?
-
Hi I'm using this code to insert items into my CComboBoxEx with icons:
BOOL CConfigDlg::OnInitDialog()
{
CDialog::OnInitDialog();m\_Images.Create(IDB\_ICONS, 16, 1, RGB (0, 128, 128)); m\_ctlCombo.SetImageList(&m\_Images); for(int j=0; j<8; j++) { CString string; string.Format (\_T ("Item %d"), j); COMBOBOXEXITEM cbei; cbei.mask = CBEIF\_IMAGE | CBEIF\_SELECTEDIMAGE | CBEIF\_TEXT; cbei.iItem = j; cbei.pszText = (LPTSTR) (LPCTSTR)string; cbei.iImage = j; cbei.iSelectedImage = j; m\_ctlCombo.InsertItem(&cbei); } return TRUE;
}
But for some reason my Combobox remains empty. Does anybody know why? thanks in advance Greg
-
Hi I'm using this code to insert items into my CComboBoxEx with icons:
BOOL CConfigDlg::OnInitDialog()
{
CDialog::OnInitDialog();m\_Images.Create(IDB\_ICONS, 16, 1, RGB (0, 128, 128)); m\_ctlCombo.SetImageList(&m\_Images); for(int j=0; j<8; j++) { CString string; string.Format (\_T ("Item %d"), j); COMBOBOXEXITEM cbei; cbei.mask = CBEIF\_IMAGE | CBEIF\_SELECTEDIMAGE | CBEIF\_TEXT; cbei.iItem = j; cbei.pszText = (LPTSTR) (LPCTSTR)string; cbei.iImage = j; cbei.iSelectedImage = j; m\_ctlCombo.InsertItem(&cbei); } return TRUE;
}
But for some reason my Combobox remains empty. Does anybody know why? thanks in advance Greg
That code looks correct to me, but there are a couple of other things I can think of:
- Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?
- Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?
As a very minor point, I don't like the
(LPTSTR)(LPCTSTR)string
cast, because I tend to think it's bad practice to cast away theconst
. How about:cbei.pszText=string.GetBuffer(0);
...
m_ctlCombo.InsertItem(&cbei);
string.ReleaseBuffer();
"We are the knights who say Ni" (The Knights Who Say Ni)
-
That code looks correct to me, but there are a couple of other things I can think of:
- Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?
- Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?
As a very minor point, I don't like the
(LPTSTR)(LPCTSTR)string
cast, because I tend to think it's bad practice to cast away theconst
. How about:cbei.pszText=string.GetBuffer(0);
...
m_ctlCombo.InsertItem(&cbei);
string.ReleaseBuffer();
"We are the knights who say Ni" (The Knights Who Say Ni)
Mike Upton wrote: Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?
void CConfigDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConfigDlg)
DDX_Control(pDX, IDC_COMBO, m_ctlCombo);
...Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?
class CConfigDlg : public CDialog
{
public:
CConfigDlg(CWnd* pParent = NULL);
//{{AFX_DATA(CConfigDlg)
enum { IDD = IDD_CONFIG };
CComboBoxEx m_ctlCombo;I don't know what's wrong :confused:
-
Mike Upton wrote: Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?
void CConfigDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConfigDlg)
DDX_Control(pDX, IDC_COMBO, m_ctlCombo);
...Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?
class CConfigDlg : public CDialog
{
public:
CConfigDlg(CWnd* pParent = NULL);
//{{AFX_DATA(CConfigDlg)
enum { IDD = IDD_CONFIG };
CComboBoxEx m_ctlCombo;I don't know what's wrong :confused:
-
Mike Upton wrote: Is the m_ctlCombo variable hooked up to the actual control properly? Is there a DDX_Control call for it in the CConfigDlg::DoDataExchange method?
void CConfigDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConfigDlg)
DDX_Control(pDX, IDC_COMBO, m_ctlCombo);
...Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?
class CConfigDlg : public CDialog
{
public:
CConfigDlg(CWnd* pParent = NULL);
//{{AFX_DATA(CConfigDlg)
enum { IDD = IDD_CONFIG };
CComboBoxEx m_ctlCombo;I don't know what's wrong :confused:
Gregor S. wrote: _Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?
class CConfigDlg : public CDialog
{
public:
CConfigDlg(CWnd* pParent = NULL);
//{{AFX_DATA(CConfigDlg)
enum { IDD = IDD_CONFIG };
CComboBoxEx m_ctlCombo;_
Sorry, that's not what I meant. What I meant was, did you insert the control into the dialog resource (in the dialog resource editor) as a standard combo box or an extended combobox? I've just created a test dialog based project using the AppWizard, added an extended combo box to the dialog (and used the wizard to add a control variable for it), added a bitmap to use for the images and an image list member in the dialog, then copied and pasted your code into the OnInitDialog method and everything works perfectly.
"We are the knights who say Ni" (The Knights Who Say Ni)
-
Gregor S. wrote: _Mike Upton wrote: Is the control on the dialog resource actually a ComboBoxEx, or is it only a standard ComboBox?
class CConfigDlg : public CDialog
{
public:
CConfigDlg(CWnd* pParent = NULL);
//{{AFX_DATA(CConfigDlg)
enum { IDD = IDD_CONFIG };
CComboBoxEx m_ctlCombo;_
Sorry, that's not what I meant. What I meant was, did you insert the control into the dialog resource (in the dialog resource editor) as a standard combo box or an extended combobox? I've just created a test dialog based project using the AppWizard, added an extended combo box to the dialog (and used the wizard to add a control variable for it), added a bitmap to use for the images and an image list member in the dialog, then copied and pasted your code into the OnInitDialog method and everything works perfectly.
"We are the knights who say Ni" (The Knights Who Say Ni)