Simple ComboBox Problems
-
I'm writing an SDI application which uses a dialog box to prompt the user. In this dialogbox I have a 2 ComboBoxes which need to be populated with different data every time the dialog is called. I have two problems: 1. This is the major one. Although I get no errors, and I've stepped through the code, the ComboBoxes always remain empty. I've included code at the bottom to show what I'm doing at the moment. 2. This is more a coding design question. Where is the best place to put this code? I can't put it in the Class Constructor because that causes an assertion error (No hWnd?). At the moment I'm trapping the ShowWindow Message and setting a flag to say whether or not I've already added stuff to the ComboBoxes Any Help would be appreciated, - X // Populate Combo Boxes CComboBox* temp1; CComboBox* temp2; int i; temp1 = (CComboBox *)GetDlgItem(IDC_MEM1); // IDC_MEM1 and 2 are the ID's temp2 = (CComboBox *)GetDlgItem(IDC_MEM2); // Of the combo boxes for (i = 0; i < 32; i++) { temp1->AddString((LPCTSTR)m_pDoc->MemoryLocations[i]); temp2->AddString((LPCTSTR)m_pDoc->MemoryLocations[i]); }
-
I'm writing an SDI application which uses a dialog box to prompt the user. In this dialogbox I have a 2 ComboBoxes which need to be populated with different data every time the dialog is called. I have two problems: 1. This is the major one. Although I get no errors, and I've stepped through the code, the ComboBoxes always remain empty. I've included code at the bottom to show what I'm doing at the moment. 2. This is more a coding design question. Where is the best place to put this code? I can't put it in the Class Constructor because that causes an assertion error (No hWnd?). At the moment I'm trapping the ShowWindow Message and setting a flag to say whether or not I've already added stuff to the ComboBoxes Any Help would be appreciated, - X // Populate Combo Boxes CComboBox* temp1; CComboBox* temp2; int i; temp1 = (CComboBox *)GetDlgItem(IDC_MEM1); // IDC_MEM1 and 2 are the ID's temp2 = (CComboBox *)GetDlgItem(IDC_MEM2); // Of the combo boxes for (i = 0; i < 32; i++) { temp1->AddString((LPCTSTR)m_pDoc->MemoryLocations[i]); temp2->AddString((LPCTSTR)m_pDoc->MemoryLocations[i]); }
hi.. * the right place to initalise a dialog box would be "WM_INITDIALOG" / "OnInitDialog"... but this function is just called once (like a constructor.. but the HWND is already defined) (btw.. for deconstruction "WM_DESTROY" / "OnDestry" would be the right place.. there the HWND is still defined) * to your coding question.. i don't know why this doesn't work.. i use pretty the same code.. i am just using membervars of the type "control".. and this works absolutely fine for me.. the first thing i would look at is, if adding works if you put constant strings in it (like temp->AddString("Hello");) if this works your m_pDoc->MemoryLocations[i] will not work. hope i could help you a little bit.. Bernhard "After twelve years of therapy my psychiatrist said something that brought tears to my eyes. He said, 'No hablo ingles.'" Ronnie Shakes
-
I'm writing an SDI application which uses a dialog box to prompt the user. In this dialogbox I have a 2 ComboBoxes which need to be populated with different data every time the dialog is called. I have two problems: 1. This is the major one. Although I get no errors, and I've stepped through the code, the ComboBoxes always remain empty. I've included code at the bottom to show what I'm doing at the moment. 2. This is more a coding design question. Where is the best place to put this code? I can't put it in the Class Constructor because that causes an assertion error (No hWnd?). At the moment I'm trapping the ShowWindow Message and setting a flag to say whether or not I've already added stuff to the ComboBoxes Any Help would be appreciated, - X // Populate Combo Boxes CComboBox* temp1; CComboBox* temp2; int i; temp1 = (CComboBox *)GetDlgItem(IDC_MEM1); // IDC_MEM1 and 2 are the ID's temp2 = (CComboBox *)GetDlgItem(IDC_MEM2); // Of the combo boxes for (i = 0; i < 32; i++) { temp1->AddString((LPCTSTR)m_pDoc->MemoryLocations[i]); temp2->AddString((LPCTSTR)m_pDoc->MemoryLocations[i]); }
First remark: read essay of Joseph Newcomer on this web site under C++/MFC/STL>>General about the use of GetDlgItem. Using MFC the right way doesn't require GetDlgItem calls. Give a control variable names to your controls: c_cbxMem1 to IDC_MEM1 c_cbxMem2 to IDC_MEM2 Adding strings to comboboxes is done by: c_cbxMem1.AddString(sMyString) But this is not the reason why your code doesn't work. Perhaps you didn't initialize the m_pDoc pointer the right way. Put the handler to invoke your dialog in the application Document and initialize the member m_pDoc as follows: ...OnMyDialog() { CDialog myDlg; myDlg.m_pDoc = this; myDlg.DoModal(); ... } and if your strings in the application document are intialized correctly the AddString() function in the comboboxes should work fine. I hope this will help.
-
First remark: read essay of Joseph Newcomer on this web site under C++/MFC/STL>>General about the use of GetDlgItem. Using MFC the right way doesn't require GetDlgItem calls. Give a control variable names to your controls: c_cbxMem1 to IDC_MEM1 c_cbxMem2 to IDC_MEM2 Adding strings to comboboxes is done by: c_cbxMem1.AddString(sMyString) But this is not the reason why your code doesn't work. Perhaps you didn't initialize the m_pDoc pointer the right way. Put the handler to invoke your dialog in the application Document and initialize the member m_pDoc as follows: ...OnMyDialog() { CDialog myDlg; myDlg.m_pDoc = this; myDlg.DoModal(); ... } and if your strings in the application document are intialized correctly the AddString() function in the comboboxes should work fine. I hope this will help.
Just a wild stab-in-the dark, but when i first used combo boxes, they always appeared empty to me. Then I finally twigged that you have to drag out the size of the dropped down list in the dialog editor. i hadn't done this, and my dropped down list was so small it appeared empty. Sorry to dissapoint you all with my lack of a witty or poignant signature.