WxListBox1->Append(const wxString &Item, void *client Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
liangbowen
Knows some wx things
Knows some wx things
Posts: 31
Joined: Wed May 24, 2006 12:45 am
Contact:

WxListBox1->Append(const wxString &Item, void *client

Post by liangbowen »

How Can i Add an Int clientdata to listbox item?
this is how i do, but doesn't work at all.

void testwxdlgDlg::WxButton1Click(wxCommandEvent& event)
{
int a=10;

WxListBox1->Append("cccc", &a);
a = 13;
WxListBox1->Append("aaaa", &a);
a = 22;
WxListBox1->Append("bbbb", &a);
}

//select one listbox item
void testwxdlgDlg::WxListBox1Selected(wxCommandEvent& event)
{
char temp[20];
sprintf(temp,"%d",(int*)event.GetClientData());
MessageBox(NULL,event.GetString(),temp,MB_OKCANCEL);

}
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

You cannot use pointers/references on local variables as client data. These are lost when you exit the function.

Either you save them in an array (or any other structure) in a class member object,
or you convert the [const] values into void* ...

Code: Select all

void testwxdlgDlg::WxButton1Click(wxCommandEvent& event) 
{ 
int a=10; 

WxListBox1->Append("cccc", (void*)a); 
a = 13; 
WxListBox1->Append("aaaa", (void*)a); 
a = 22; 
WxListBox1->Append("bbbb", (void*)a); 
} 
liangbowen
Knows some wx things
Knows some wx things
Posts: 31
Joined: Wed May 24, 2006 12:45 am
Contact:

Post by liangbowen »

I know I can't use local variables. But I thought that the Append() will create another copy of data on heap(which I don't know if it's true) before the function exits.

And then I later evoke a select item event, say "WxListBox1Selected(wxCommandEvent& event) {}", it will retrun the pointer to the data on heap to me via event.GetClientData(). (which I don't know if it's true either)

I also tried:

Code: Select all

int a;
void testwxdlgDlg::WxButton1Click(wxCommandEvent& event) {
 WxListBox1->Append("cccc", (void*)&a); 
a = 13; 
WxListBox1->Append("aaaa", (void*)&a); 
a = 22; 
WxListBox1->Append("bbbb", (void*)&a); 
}
void testwxdlgDlg::WxListBox1Selected(wxCommandEvent& event) 
{ 
char temp[20]; 
sprintf(temp,"%d",(int*)event.GetClientData()); 
MessageBox(NULL,event.GetString(),temp,MB_OKCANCEL); 
}
yet still can't work, I just want a simple example tell me how to associate Int values with listbox items, and how to get the values back when I later select items.

do I have to create a global int array say int a[10] and write codes as follow? :

Code: Select all

int a[10];
void testwxdlgDlg::WxButton1Click(wxCommandEvent& event) {
 WxListBox1->Append("cccc", (void*)&a); 
a[0] = 13; 
WxListBox1->Append("aaaa", (void*)&a); 
a[1] = 22; 
WxListBox1->Append("bbbb", (void*)&a); 
a[2] = 32;
}
void testwxdlgDlg::WxListBox1Selected(wxCommandEvent& event) 
{ 
char temp[20]; 
sprintf(temp,"%d",a[event.GetInt()]);
MessageBox(NULL,event.GetString(),temp,MB_OKCANCEL); 
}
I am sure it will work, but that seems stupid.
Last edited by liangbowen on Wed May 24, 2006 2:11 pm, edited 1 time in total.
Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor »

IMHO wxListBox (or others) dont copy any data.
If you want to save a data, you must allocate it on the heap and assign the address as data.
If your data is just an integer, you can assign it direclyt with casting.
What is little and green, witch go up and down ??
Yoda playing with the force.
liangbowen
Knows some wx things
Knows some wx things
Posts: 31
Joined: Wed May 24, 2006 12:45 am
Contact:

Post by liangbowen »

I am sorry benedicte, you are right!
I just notice you've worte (void*)a, not (void*)&a.
or you convert the [const] values into void*
yeah, that's right.
thanks very much benedicte.
Post Reply