CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2005
    Posts
    59

    Problem with ComboBox

    Ok I have a data structure as follows

    Code:
    typedef struct
    {
    	char ConstructionName[20];
    }Constructions;
    I have a dialog box with a combo box which wants to use values in this structure. On the dialog init I load up this data structure from a data file as follows

    Code:
    char * pConstructionArchive;
    	int j;
            Constructions* pConstructionData = new Constructions;
    	//Load the data
    	ifstream file("constructions.dat", ios::in|ios::binary);
    	file.seekg (0, ios::end);
    	int size = file.tellg();
    	file.seekg(0,ios::beg);
    	pConstructionArchive = new char [size];
    	file.read (pConstructionArchive, size);
    	file.close();
            long * header = reinterpret_cast<long *>(&pConstructionArchive[0]);
    	long ConstructionSig = header[0];
    	long NumConstructions = header[2];
    	long ConDataStart = header[3];
    	char * pConDataArchive = &pConstructionArchive[ConDataStart];
    I then loop through the elements and populate the comboBox as follows

    Code:
    if(NumConstructions>0)
    	{
    		for (j=0;j<NumConstructions;j++)
    		{
    			char * pHeaderLocation;
    			pHeaderLocation = &pConDataArchive[(j*20)];
    			memcpy(pConstructionData->ConstructionName, pHeaderLocation,20);
    			intIndex = m_addFabricConstruction.AddString(pConstructionData->ConstructionName);
    			m_addFabricConstruction.SetItemData(intIndex,(DWORD)pConstructionData);
    		}
    	}
    	
        //set the appropriate one as current selection
        m_addFabricConstruction.SetCurSel(0);
    Finally I have an on selection change as follows

    Code:
    void CAddFabric::OnCbnSelchangeFabricConstruction()
    {
    //Get the current value se we can write new data to file
    	int nCurSel = m_addFabricConstruction.GetCurSel();
    	CString result = (LPCSTR)m_addFabricConstruction.GetItemData(nCurSel);
    }
    Now the problem is that no matter what I change the value in the comboBox to I get the same value for result. Stepping through the debugger it seems that intIndex gets stuck at 2 and never increments anymore.. I am wondering if its that or something else I am doing wrong..

    Thanks for any advice...Andrew

  2. #2
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Problem with ComboBox

    What you are seeing may actually be correct. If your ComboBox is sorted, then adding an item will automatically trigger a resort, thus it would depend on the sort order of the items you're adding.

    If you set the ComboBox to unsorted, you'll probably see what you are expecting.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  3. #3
    Join Date
    Aug 2005
    Posts
    59

    Re: Problem with ComboBox

    Good idea, and I thinkit explains why I was seing the intIndex values I was seing. However, changing the combobox to not sort while it did sort out what I was seing in the debug did nothing to change the fact that result is always equal to the same thing...

    The 4 values loaded are knit ; non woven; woven; and other

    Result is always = to other no matter what I select in the combobox...

    I have comfirmed that the value of nCurSel is changing but it does not affect the output result.

    Thanks A

  4. #4
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Problem with ComboBox

    The basic problem seems to be that what you have set for ItemData on all entries is the pConstructionData pointer. This means that all of your entries point to the same memory location.

    Since pConstructionData can only hold one item at a time. It changes as you initially load the data into the ComboBox, but when you've finished loading, it holds the last item you entered ("other").

    Therefore when you try to retrieve the ItemData, it returns the pConstructionData pointer, which still contains "other".

    You need some kind of array or collection of your Constructions so that you can access them later.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured