wxListbox->Delete issue

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.
Chris-Ediss
In need of some credit
In need of some credit
Posts: 8
Joined: Mon Apr 16, 2018 2:35 pm

wxListbox->Delete issue

Post by Chris-Ediss »

Newbie question
Selecting item 'i' in a wxListbox raises a Select event handler
as expected. wxListbox->Delete(i) does delete the entry 'i' but
it also raises a second Select event with index 0.
What is the purpose of this second event? An explanation would
be appreciated.

Intel i5, Linux Mint 18.2, Code::Blocks 13.12, wxWidgets 3.02
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxListbox->Delete issue

Post by ONEEYEMAN »

Hi,
In a list box if you make a selection, the selection can not be turned off.
So if you select the item, the selection stays.

The problem is that in your case the selection changes for some reason.

It is also possible that since you delete an item, the control doesn't know which item to select. And so it is selecting the very first item, i.e. item 0.

Can you replicate it in the widgets sample?

There is also a Deselect() function which should remove the selection from the item, but unless called explicitly, selection stays with the control.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxListbox->Delete issue

Post by doublemax »

Please check if this happens in the "listbox" section of the "widgets" sample (there is a log window which shows selection events).

If not, please show the code of the event handler. Also, which event are you catching?
Use the source, Luke!
Chris-Ediss
In need of some credit
In need of some credit
Posts: 8
Joined: Mon Apr 16, 2018 2:35 pm

Re: wxListbox->Delete issue

Post by Chris-Ediss »

Thanks for the feedback. Here is some more information to clarify
the situation.

The goal is to extract a sublist of names from ListBox1
and transfer them to ListBox2

Here is the bare bones of the Select event handler

void wxWidgetDemoFrame::OnListBox1Select(wxCommandEvent& event)
{
wxString l1;
int i1 = ListBox1->GetSelection(); // Get the selected item index
l1 = ListBox1->GetString(i1); // Get the selected string
ListBox2->Append(l1); // Add that string to Listbox2
ListBox1->Delete(i1); // This does delete item l1
// But - On exit from this handler a new Select event occurs with index 0
// and the existing string item 0 is also transferred.
}

I have tried several workarounds such as :-
ListBox1->GTKDisableEvents();
ListBox1->Delete(i1);
ListBox1->GTKEnableEvents();
This kind of worked but disabled all further Select events.

ListBox1->Insert(wxT("***Dummy***"),0);
I also tried putting a dummy string in index 0 at the end of the handler
and recognising it at the beginning so the spurious event could be ignored.
But how ugly is that !

I gather that the Delete function is inherited from wxControlWithItems
and is intended to operate this way for some unknown reason.
Perhaps we need a ListBox member function 'Remove' or something.
Your thoughts are appreciated.

Intel i5, Linux Mint 18.2, Code::Blocks 13.12, wxWidgets 3.02
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxListbox->Delete issue

Post by doublemax »

Again:
Please check if this happens in the "listbox" section of the "widgets" sample (there is a log window which shows selection events).
Are you sure the event you receive is from ListBox1 and not ListBox2?
What happens if you comment out "ListBox2->Append"?
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxListbox->Delete issue

Post by ONEEYEMAN »

Hi,
Also, FYI, this forum is for wx-users by wx-users.

If you want to discuss changes to the library itself - you need to send an e-mail to wx-dev ML where core wx developers (Vadim Zeitlin) are.

Thank you.
Chris-Ediss
In need of some credit
In need of some credit
Posts: 8
Joined: Mon Apr 16, 2018 2:35 pm

Re: wxListbox->Delete issue

Post by Chris-Ediss »

Thanks doublemax for your response.
Commenting out all reference to ListBox2 does not help.
We still get the second Select event with index 0.

I did notice your suggestion to check the
"listbox" section of the "widgets" sample
(there is a log window which shows selection events).
but i confess that I have not come across the
"widgets sample" to which you refer. Where is that please?
Chris.E - Intel i5, Linux Mint 18.2, Code::Blocks 13.12, wxWidgets 3.02
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxListbox->Delete issue

Post by ONEEYEMAN »

Hi,
This sample as well as any other is located in c:\wxWidgets-x.x.x\samples directory.

You must build the samples with the exactly the same command as you build wxWidgets (if you use MinGW/MSYS) or with the exact same configuration (if you use MSVC).

Thank you.
Chris-Ediss
In need of some credit
In need of some credit
Posts: 8
Joined: Mon Apr 16, 2018 2:35 pm

Re: wxListbox->Delete issue

Post by Chris-Ediss »

OK, I found the examples at usr/share/doc/wx3.0-examples/examples/samples
on my Linux Mint machine. I couldn't see Listbox but copied the ListCtrl
dircectory to my Home. Issuing a make chokes because 'sample.xpm' could
not be found. I'll work more on that later.
Meanwhile I tried another approach. Mirror the two listbox contents in
two string arrays inList and outList. To avoid using the troublesome
Listbox->Delete function delete the item from the string array
'inList.RemoveAt(i1)'. Then Clear the listbox and re-load with the updated
string array. This almost works. The first selection gets transferred but
item '0' is also transferred. Subsequent selections seem to work ok. The
troublesome code appears to be the Listbox1->Clear call. This is also not
a member function but is also inherited from wxControlWithItems (?).

inList and outList are declared globally. inList and Listbox1 are pre-loaded
with a bunch of strings, ListBox2 is initially empty.

Code: Select all

void wxWidgetDemoFrame::OnListBox1Select(wxCommandEvent& event)
{
    wxString l1;
    int i = ListBox1->GetCount();
    if(i > 0)
    {
        int i1 = ListBox1->GetSelection();        // Get the selected item index
        l1 = ListBox1->GetString(i1);             // Get the selected string
        ListBox2->Insert(l1, 0);
        outList.Insert(l1, 0);
        inList.RemoveAt(i1);
        ListBox1->Clear();
        if(inList.GetCount() > 0) ListBox1->InsertItems(inList, 0);
    }
}

void wxWidgetDemoFrame::OnListBox2Select(wxCommandEvent& event)
{
    wxString l2;
    int i = ListBox2->GetCount();
    if(i > 0)
    {
        int i2 = ListBox2->GetSelection();        // Get the selected item index
        l2 = ListBox2->GetString(i2);             // Get the selected string
        ListBox1->Insert(l2, 0);
        inList.Insert(l2, 0);
        outList.RemoveAt(i2);
        ListBox2->Clear();
        if(outList.GetCount() > 0) ListBox2->InsertItems(outList, 0);
    }
}
Getting close, any suggestions would be appreciated.

Intel i5, Linux Mint 18.2, Code::Blocks 13.12, wxWidgets 3.02
Chris.E - Intel i5, Linux Mint 18.2, Code::Blocks 13.12, wxWidgets 3.02
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxListbox->Delete issue

Post by ONEEYEMAN »

Hi,
You should build the widgets sample in place and then run it. Do not copy the sample anywhere.
When it ran, on the right hand side there will be a tree control. In there select "listbox" and on the right hand side you will see the listbox panel and a third panel with some options.

Add some lines and then delete some.

Is it reproducible?

Thank you.
Chris-Ediss
In need of some credit
In need of some credit
Posts: 8
Joined: Mon Apr 16, 2018 2:35 pm

Re: wxListbox->Delete issue

Post by Chris-Ediss »

Thanks ONEEYEMAN I have the ListCtrl sample working. Deleting and re-appending items works ok.
I don't think the sample I have is quite the same as you mentioned. It has just 3 columns on the left
with items below, no tree on the right, and no mention of ListBox.

I tried the makefile in the parent Samples directory thinking that might create an index for the Samples but that failed.
It seems that all the object and cpp files in all the samples have to be unpacked first.

Looking at the Samples directory the closest I can see is ListCtrl but no ListBox.

Making progress though, Thanks.
Chris.E - Intel i5, Linux Mint 18.2, Code::Blocks 13.12, wxWidgets 3.02
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxListbox->Delete issue

Post by ONEEYEMAN »

Hi,
There is no listbox sample as you noted.
If you want to check wxListBox functionality you should build "widgets" sample (yes, that's how it is called).
Also - wxListCtrl and wxListBox are 2 different controls, so you can't really compare functionality with them.

Thank you.
Chris-Ediss
In need of some credit
In need of some credit
Posts: 8
Joined: Mon Apr 16, 2018 2:35 pm

Re: wxListbox->Delete issue

Post by Chris-Ediss »

OK, I have the 'widgets' demo running as you described ONEEYEMAN Thanks.
The Listbox section works fine. Adding and deleting items work reliably.

Now back to my original question, why does my simple listbox transfer not work.
I'll scan the '.h' and '.cpp' files to see if I can see any errors or omissions.

Making more progress, thanks guys.
Chris.E - Intel i5, Linux Mint 18.2, Code::Blocks 13.12, wxWidgets 3.02
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxListbox->Delete issue

Post by ONEEYEMAN »

Hi,
2 questions:
1. Did you compile wxWidgets yourself or install it from the repository?
2. Do you have a handler for the item selection?

If you have a handler for it try to set a breakpoint in it and run under gdb.
When the breakpoint is hit, check the backtrace. The backtrace should tell you where the first and second events are coming from.

For that - install gdb (Linux debugger) and you will need to either install the development version of wxWidgets or compile the library yourself in debug mode and then recompile the application in debug mode as well.

Thank you.
Chris-Ediss
In need of some credit
In need of some credit
Posts: 8
Joined: Mon Apr 16, 2018 2:35 pm

Re: wxListbox->Delete issue

Post by Chris-Ediss »

Hello ONEEYEMAN

I installed wxWidgets using Synaptic. The repositoy settings are :-
Main - packages.linuxmint.com and Base - archive.ubuntu.com/ubuntu

The packages installed are wx3.0-doc, wx3.0-examples, wx3.0-headers,
wx3.0-i18n, wx-common, libwxbase3.0-Ov5, libwxbase3.0-dev,
libwxgtk3.0-dev, Python-wxgtk3.0, libwxgtk-media3.0
... not all of which are necessary I think (I don't use Python).

I am using the Code::Blocke IDE which is very slick. It has it's own
de-bugger so I'll drop a few checkpoints in my code to see if that
brings anything to light.

As to your second question, The code for my ListBox Select event handler
is posted a couple of sessions earlier, if that is what you meant.

I have been using Bruce Perens book 'Cross Platform GUI Programming
with WxWidgets' as a reference. It lists wxListBox member functions
on p98 but does not include Delete and Clear directly. It implies that
those functions are inherited from wxControlWithItems. That section
on p54 says that 'Delete removes an item using a zero-based position.'
This sounds suspiciously like the symptoms I see. Delete does remove
the item indicated by it's integer argument but then a second
Select event is issued with with index 0. More later.
Chris.E - Intel i5, Linux Mint 18.2, Code::Blocks 13.12, wxWidgets 3.02
Post Reply