Thread: Problem trying to retrieve data from combo box! (strange issue)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #16
    Registered User
    Join Date
    Jan 2010
    Posts
    209

    Thumbs up and finally....

    John your insight has led to a proper solution to the problem. I disabled the WM_ACTIVE message block and and then duplicated the WM_INITDIALOG code as I think I mentioned above.

    Then by trail and error I just turned on and off a variety of functions in that code until I could get it to the minimum needed to run correctly. I added a return statement to the WM_INITDIALOG block too so this code only runs once. I think before it just dropped through the switch statement on init and into the WM_ACTIVE block because I removed the return.

    Anyways....to cut a long story short it ran properly when the function setResolution was run twice. I'll post the original below:

    Code:
    void setResolution(HWND hDlg)
    {   
        D3DObj.mComboResData.clear();
        D3DClass::resData _resData;
        int j = 0;  
    
        for (int i = 0; i < D3DObj.mResolutionPair.size(); i += 4)
        {
            _resData.w = D3DObj.mResolutionPair[i];
            _resData.h = D3DObj.mResolutionPair[i + 1];
            _resData.Hz = (double)(D3DObj.mResolutionPair[i + 2]) / D3DObj.mResolutionPair[i + 3];
    
            std::wstring _resolution = std::to_wstring(_resData.w) +
                L" x " + std::to_wstring(_resData.h);
    
            const WCHAR* resolution = _resolution.c_str();
            D3DObj.mComboResData.push_back(_resData);
    
            WPARAM nI = (WPARAM)SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_ADDSTRING, (WPARAM)0, (LPARAM)resolution);
            SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_SETITEMDATA, (WPARAM)nI, (LPARAM) & (D3DObj.mComboResData[j])); // change nI variable to 0 and it works (for first entry)
            j++;       
    
            #ifdef SKIP  // change to _DEBUG to display
                std::wstring _string = std::to_wstring(_resData.w) + L" "
                    + std::to_wstring(_resData.h) + L"\n"
                    + std::to_wstring(_resData.Hz) + L"\n"
                    + std::to_wstring(nI) + L"\n\n"; // may as well print nI, too
                OutputDebugString(_string.c_str());
            #endif
        }
    
        return;
    }
    I wrote a duplicate of it called pingCombo which I used to replace the first call to setRefresh. Then I cannibalised everything in it down to the bare minimum to get it to work. I even noticed if I messed with the iterator size in the for loop it would affect whether or not it worked. Confusing but I knew I was still getting close.

    Well....turns out I needed to completely fill out the mComboResData vector before running the for loop which uses the indexing for the combo box data stuff. Here's the new version of setResolution:

    (note the new for loop at the top)

    Code:
    void setResolution(HWND hDlg)
    {   
        D3DObj.mComboResData.clear();
        D3DClass::resData _resData;
        int j = 0;
    
        for (int i = 0; i < D3DObj.mResolutionPair.size(); i += 4)
        {        
            D3DObj.mComboResData.push_back(_resData);       
        }
    
        for (int i = 0; i < D3DObj.mResolutionPair.size(); i += 4)
        {
            _resData.w = D3DObj.mResolutionPair[i];
            _resData.h = D3DObj.mResolutionPair[i + 1];
            _resData.Hz = (double)(D3DObj.mResolutionPair[i + 2]) / D3DObj.mResolutionPair[i + 3];
    
            std::wstring _resolution = std::to_wstring(_resData.w) +
                L" x " + std::to_wstring(_resData.h);
    
            const WCHAR* resolution = _resolution.c_str();
            D3DObj.mComboResData[j] = _resData;
    
            WPARAM nI = (WPARAM)SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_ADDSTRING, (WPARAM)0, (LPARAM)resolution);
            SendMessage(GetDlgItem(hDlg, IDC_COMBO1), CB_SETITEMDATA, (WPARAM)nI, (LPARAM) & (D3DObj.mComboResData[j])); // change nI variable to 0 and it works (for first entry)
            j++;       
    
            #ifdef SKIP  // change to _DEBUG to display
                std::wstring _string = std::to_wstring(_resData.w) + L" "
                    + std::to_wstring(_resData.h) + L"\n"
                    + std::to_wstring(_resData.Hz) + L"\n"
                    + std::to_wstring(nI) + L"\n\n"; // may as well print nI, too
                OutputDebugString(_string.c_str());
            #endif
        }
    
        return;
    }
    And that finally...is it. The struct resData is defined in the separate class with initialized variables, so this way of filling up the vector doesn't throw any errors. Then it's just a case of using the typical array indexing on the vector rather than push_back function and that's the end of the matter.

    I still don't know exactly why this works but it's obviously the cause and now there are no longer any issues with the indexing, and the code doesn't have to run twice to work.

    If anyone can mop up that last little mystery of why this works that would be most helpful.

    Thanks
    Last edited by shrink_tubing; 12-02-2023 at 02:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to store and retrieve data with a Query function in C
    By JiaWei Lee in forum C Programming
    Replies: 0
    Last Post: 10-22-2016, 02:17 AM
  2. How to parse the data and retrieve the data.
    By kane_a in forum C Programming
    Replies: 11
    Last Post: 12-08-2005, 09:11 AM
  3. using class to retrieve and store data
    By FoodDude in forum C++ Programming
    Replies: 9
    Last Post: 08-19-2005, 07:50 AM
  4. Combo Box in MFC, how to randomize data of it...please help!
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 06-24-2002, 02:26 AM
  5. Retrieve specific data from text for edit
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-22-2002, 09:02 AM

Tags for this Thread