Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

wxEVT_COMMAND_LIST_ITEM_SELECTED occurs twice

52 views
Skip to first unread message

Christian Hackl

unread,
Nov 30, 2009, 5:30:19 AM11/30/09
to
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
ha...@sbox.tugraz.at

Milano 2008/2009 -- L'Italia chiamᅵ, sᅵ!

Vadim Zeitlin

unread,
Jan 24, 2010, 6:19:17 AM1/24/10
to
On 2009-11-30, Christian Hackl <ha...@sbox.tugraz.at> wrote:
> 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.

> 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/

0 new messages