GetItemDataPtr (ComboBox) Not Working For Me
-
hello guys... I am trying to get and set the data in the combo box using
GetItemDataPtr
andSetItemDataPtr
. I am facing problem when I use the functionGetItemDataPtr
. It return bad pointer. Here is what I do to add stringint 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.
-
hello guys... I am trying to get and set the data in the combo box using
GetItemDataPtr
andSetItemDataPtr
. I am facing problem when I use the functionGetItemDataPtr
. It return bad pointer. Here is what I do to add stringint 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.
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
-
hello guys... I am trying to get and set the data in the combo box using
GetItemDataPtr
andSetItemDataPtr
. I am facing problem when I use the functionGetItemDataPtr
. It return bad pointer. Here is what I do to add stringint 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.
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
-
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
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.
-
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
-
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.
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
-
hello guys... I am trying to get and set the data in the combo box using
GetItemDataPtr
andSetItemDataPtr
. I am facing problem when I use the functionGetItemDataPtr
. It return bad pointer. Here is what I do to add stringint 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.
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