Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetItemDataPtr (ComboBox) Not Working For Me

GetItemDataPtr (ComboBox) Not Working For Me

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasehelp
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    hello guys... I am trying to get and set the data in the combo box using GetItemDataPtr and SetItemDataPtr. I am facing problem when I use the function GetItemDataPtr . It return bad pointer. Here is what I do to add string

    int iPosition = 0;
    CString sName;
    sName = "Ali";

    for (int nIndex = 0; nIndex < 5; nIndex++)
    {
    iPosition = m_ComboNames->AddString(sName);
    ASSERT (iPosition != CB_ERR);
    ComboNames->SetItemDataPtr(iPosition, (void*)sName);
    }

    and to get the data from the combo box I do as under.

    int index = m_Combo->GetCurSel();

    CString str;
    LPVOID ptr;

    if(index != LB_ERR)
    {
    ptr = m_Combo->GetItemDataPtr(index);
    str = (CString)ptr;
    MessageBox(str );
    }

    As stated ealier, it shows very strange characters. How can I cast my ptr properly in order to get the value right? thnx for any input.

    This world is going to explode due to international politics, SOON.

    T D L 3 Replies Last reply
    0
    • A AmbiguousName

      hello guys... I am trying to get and set the data in the combo box using GetItemDataPtr and SetItemDataPtr. I am facing problem when I use the function GetItemDataPtr . It return bad pointer. Here is what I do to add string

      int iPosition = 0;
      CString sName;
      sName = "Ali";

      for (int nIndex = 0; nIndex < 5; nIndex++)
      {
      iPosition = m_ComboNames->AddString(sName);
      ASSERT (iPosition != CB_ERR);
      ComboNames->SetItemDataPtr(iPosition, (void*)sName);
      }

      and to get the data from the combo box I do as under.

      int index = m_Combo->GetCurSel();

      CString str;
      LPVOID ptr;

      if(index != LB_ERR)
      {
      ptr = m_Combo->GetItemDataPtr(index);
      str = (CString)ptr;
      MessageBox(str );
      }

      As stated ealier, it shows very strange characters. How can I cast my ptr properly in order to get the value right? thnx for any input.

      This world is going to explode due to international politics, SOON.

      T Offline
      T Offline
      TomasRiker2
      wrote on last edited by
      #2

      Probably your CString object, to which you store a pointer, doesn't exist any more when you're reading the pointer back. It looks like "sName" is declared locally and will get destroyed once the scope is exited. Then your pointer points to a region of memory that now contains something else. You can store a pointer to a CString inside your ComboBox. But you have to create this CString instance with new and not locally on the stack! And don't forget to delete it later when you don't need it any more.

      Visit my project: Derivative Calculator

      L 1 Reply Last reply
      0
      • A AmbiguousName

        hello guys... I am trying to get and set the data in the combo box using GetItemDataPtr and SetItemDataPtr. I am facing problem when I use the function GetItemDataPtr . It return bad pointer. Here is what I do to add string

        int iPosition = 0;
        CString sName;
        sName = "Ali";

        for (int nIndex = 0; nIndex < 5; nIndex++)
        {
        iPosition = m_ComboNames->AddString(sName);
        ASSERT (iPosition != CB_ERR);
        ComboNames->SetItemDataPtr(iPosition, (void*)sName);
        }

        and to get the data from the combo box I do as under.

        int index = m_Combo->GetCurSel();

        CString str;
        LPVOID ptr;

        if(index != LB_ERR)
        {
        ptr = m_Combo->GetItemDataPtr(index);
        str = (CString)ptr;
        MessageBox(str );
        }

        As stated ealier, it shows very strange characters. How can I cast my ptr properly in order to get the value right? thnx for any input.

        This world is going to explode due to international politics, SOON.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Is sName a global or member variable?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

        A 1 Reply Last reply
        0
        • D David Crow

          Is sName a global or member variable?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

          A Offline
          A Offline
          AmbiguousName
          wrote on last edited by
          #4

          DavidCrow wrote:

          Is sName a global or member variable?

          Its a local variable within the function.

          This world is going to explode due to international politics, SOON.

          D 1 Reply Last reply
          0
          • T TomasRiker2

            Probably your CString object, to which you store a pointer, doesn't exist any more when you're reading the pointer back. It looks like "sName" is declared locally and will get destroyed once the scope is exited. Then your pointer points to a region of memory that now contains something else. You can store a pointer to a CString inside your ComboBox. But you have to create this CString instance with new and not locally on the stack! And don't forget to delete it later when you don't need it any more.

            Visit my project: Derivative Calculator

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            you are right this time. but the problem is simple and basic. :zzz:

            1 Reply Last reply
            0
            • A AmbiguousName

              DavidCrow wrote:

              Is sName a global or member variable?

              Its a local variable within the function.

              This world is going to explode due to international politics, SOON.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Which means it goes away when the function ends. Make it a global or member variable.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

              1 Reply Last reply
              0
              • A AmbiguousName

                hello guys... I am trying to get and set the data in the combo box using GetItemDataPtr and SetItemDataPtr. I am facing problem when I use the function GetItemDataPtr . It return bad pointer. Here is what I do to add string

                int iPosition = 0;
                CString sName;
                sName = "Ali";

                for (int nIndex = 0; nIndex < 5; nIndex++)
                {
                iPosition = m_ComboNames->AddString(sName);
                ASSERT (iPosition != CB_ERR);
                ComboNames->SetItemDataPtr(iPosition, (void*)sName);
                }

                and to get the data from the combo box I do as under.

                int index = m_Combo->GetCurSel();

                CString str;
                LPVOID ptr;

                if(index != LB_ERR)
                {
                ptr = m_Combo->GetItemDataPtr(index);
                str = (CString)ptr;
                MessageBox(str );
                }

                As stated ealier, it shows very strange characters. How can I cast my ptr properly in order to get the value right? thnx for any input.

                This world is going to explode due to international politics, SOON.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                I fail to understand your code; you have a loop setting 5 ComboBox items to the same value (all pointing to sName). What good will that do? When the ItemData collection gets filled with stuff, I expect (1) this stuff to be persisted in memory, and (2) to be all different so you can use it to discriminate the combobox items. Furthermore, I don't consider the name of a ComboBox item a good candidate for ItemData, as it is already stored inside the Combobox anyway, and there must be an easy way to get it back, without you keeping it around in some data structures yourself. IMO valid candidates for ItemData are e.g. numeric values (say ints), when the combobox itself displays their textual representation (which may vary due to localisation). :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups