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);
}
WxListBox1->Append(const wxString &Item, void *client Topic is solved
-
- Knows some wx things
- Posts: 31
- Joined: Wed May 24, 2006 12:45 am
- Contact:
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* ...
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);
}
-
- Knows some wx things
- Posts: 31
- Joined: Wed May 24, 2006 12:45 am
- Contact:
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:
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? :
I am sure it will work, but that seems stupid.
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);
}
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);
}
Last edited by liangbowen on Wed May 24, 2006 2:11 pm, edited 1 time in total.
-
- Knows some wx things
- Posts: 31
- Joined: Wed May 24, 2006 12:45 am
- Contact: