Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 735927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:32:47+00:00 2026-05-14T07:32:47+00:00

Instead of associating objects with Combo Box items, I associate long ids representing choices.

  • 0

Instead of associating objects with Combo Box items, I associate long ids representing choices. They come from a database, so it seems natural to do so anyway. Now, I persist the id and not the index of the user’s selection, so that the choice is remembered across sessions. If id no longer exists in database – no big deal. The choice will be messed up once. If db does not change, however, then it would be a great success 😉

Here is how I get the id :

chosenSomethingIndex = cmbSomething.GetCurSel();
lastSomethingId = cmbSomething.GetItemData(chosenSomethingIndex);

How do I reverse this? When I load the stored value for user’s last choice, I need to convert that id into an index. I can do:

cmbSomething.SetCurSel(chosenSomethingIndex);

However, how can I attempt (it might not exist) to get an index once I have an id?

I am looking for a reciprocal function to GetItemData

I am using VS2008, probably latest version of MFC, whatever that is.

Thank you.

EDIT:

Ah, crap. Looks like I need to do this:

for (int i = 0; i < nCount; i++) 
{
    if (nId == GetItemData(i)) 
    {
        SetCurSel(i);
        hr = S_OK;
        break;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:32:48+00:00Added an answer on May 14, 2026 at 7:32 am

    You have a function that maps item index to database ID. There is no built-in inverse for that function because the general case doesn’t have an inverse. A single data value might map to many different items in the list control; the OS doesn’t know your data values are unique.

    Your technique of searching the control items one by one is the only way to do it, unless you have additional information stored elsewhere. As you populate your combo box, you could build a reverse index in a std::map. When you add item i with database ID id, add an entry to your other data structure, too:

    SetItemData(i, id);
    reverse_index[id] = i;
    

    Then, instead of searching one item at a time, you can just look in the index, replacing your loop with this:

    std::map<DWORD_PTR, int>::iterator it = reverse_index.find(nId);
    if (it != reverse_index.end()) {
      assert(GetItemData(*it) == nId);
      SetCurSel(*it);
      hr = S_OK;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 398k
  • Answers 398k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer An alternative implementation you could use is to use the… May 15, 2026 at 3:39 am
  • Editorial Team
    Editorial Team added an answer First of all, your deduction of the problem is pretty… May 15, 2026 at 3:39 am
  • Editorial Team
    Editorial Team added an answer You might consider to use event delegation instead of adding… May 15, 2026 at 3:39 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.