Hello, I am using Visual Studio .NET 2003, using MFC. I have a problem with a CComboBox and SetItemData. I have used SetItemData before with VC5.0, but I'm having problems now. Here's what I'm doing:
I have a function like this:
In my CPropertyPage::OnInitDialog() function, I call this function several times when adding items to the box.Code:AddComboBoxItem(CComboBox *comboBox, CString csText, int value) { int n; n = comboBox->AddString(csText); comboBox->SetItemData(n, value); }
Then my DoSpecialStuff() function does certain things depending on which item is currently selected. This function also gets called when I change the selection of the combo box. I've put breakpoints in the DoSpecialStuff() function, and it does get called at the right times. Here's an example of what it has:Code:BOOL MyPropPage::OnInitDialog() { CComboBox *comboBox = (CComboBox*)GetDlgItem(IDC_COMBO1); AddComboBoxItem(comboBox, "Option 1", 1); AddComboBoxItem(comboBox, "Option 2", 2); AddComboBoxItem(comboBox, "Option 3", 3); AddComboBoxItem(comboBox, "Option 4", 4); comboBox->SetCurSel(0); DoSpecialStuff(); return CPropertyPage::OnInitDialog(); }
Now, this code works fine the first time DoSpecialStuff() is called from OnInitDialog(). But every call after that, n is being set to 0 for some reason. I've checked the return values of GetCurSel(), it is correct. I am uncertain as to why I am getting 0. It is like the combo box is 'forgetting' the data it used to have. The combo box still has all the correct text options, so it hasn't 'forgotten' those. I've used code like this before, but not in .NET. Is this a bug in .NET, or am I just making a mistake in my code somewhere? Anyone have any ideas?Code:void MyPropPage::DoSpecialStuff() { int n; CComboBox *comboBox = (CComboBox *)GetDlgItem(IDC_COMBO1); n = comboBox->GetItemData(comboBox->GetCurSel()); if (n == 1) { //... } else if (n == 2) { //... } // etc }



LinkBack URL
About LinkBacks


