Discussion:
wxEVT_COMMAND_LIST_ITEM_SELECTED occurs twice
(too old to reply)
Christian Hackl
2009-11-30 10:30:19 UTC
Permalink
Hi!

As I take it, the recommended workaround for the fact that list
selection events cannot be vetoed is to manually reset the selection to
its previous state. However, when I try to do so, the event handler is
called twice in the process. Here's a dumbed-down yet complete piece of
code illustrating the problem:

#include <wx/wx.h>
#include <wx/listctrl.h>

class MainWindow : public wxFrame
{
public:
MainWindow() :
wxFrame(0, wxID_ANY, "Double Event Test"),
list_(new wxListView(this, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxLC_LIST ))
{
Connect(list_->GetId(), wxEVT_COMMAND_LIST_ITEM_SELECTED,
wxListEventHandler(MainWindow::onSelect));

list_->InsertItem(0, "0");
list_->InsertItem(1, "1");
list_->InsertItem(2, "2");

wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(list_, 1, wxALL, 5);
SetSizer(sizer);
}

void onSelect(wxListEvent &event)
{
static bool ignore = false;
if (ignore)
return;

if (wxMessageBox("Really?", "Really?", wxYES_NO) == wxNO)
{
ignore = true;
list_->Select(0, true);
ignore = false;
list_->Select(1, false);
list_->Select(2, false);
}
}

private:
wxListView *list_;
};

class Application : public wxApp
{
public:
virtual bool OnInit()
{
MainWindow *main_window = new MainWindow;
main_window->Show(true);
return true;
}
};

IMPLEMENT_APP(Application)


When I select a list item and click "No" in the dialog box, the dialog
immediately pops up again. (Actually, the handler is called three times,
so if I did not have that "ignore" variable in the code, the dialog
would pop up three times as well.)

Somehow, the event seems to occur again immediately *after* the event
handler finishes, and I can't for the heck of it figure out why. To add
insult to injury, everything happens as it should when I step through
the very same build in a debugger.

I'm using VC++ 2008 (Version 9.0.30729.1 SP) and wxWidgets 2.8.9 on
Windows XP SP2.

Is this a bug? Am I trying to do something horribly wrong? What can I
do? Any hints appreciated.


Thanks,
Christian
--
Christian Hackl
***@sbox.tugraz.at

Milano 2008/2009 -- L'Italia chiamò, sì!
Vadim Zeitlin
2010-01-24 11:19:17 UTC
Permalink
Post by Christian Hackl
As I take it, the recommended workaround for the fact that list
selection events cannot be vetoed is to manually reset the selection to
its previous state. However, when I try to do so, the event handler is
called twice in the process.
I think you should try to filter the extra events in your own code by e.g.
ignoring any of them pertaining to the item you're already trying to return
selection to.
Post by Christian Hackl
Is this a bug? Am I trying to do something horribly wrong? What can I
do? Any hints appreciated.
There is probably a bug but I'd say that the wrong thing to do is to try
to emulate vetoing a selection event -- it's not possible for a good reason
and your users wouldn't expect it.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Loading...