wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected 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
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by shawnee »

Hi All,

I'm using single selection wxListCtrl. I found when I select a selected item again, it won't trigger wxEVT_LIST_ITEM_SELECTED event.
How can I get wxEVT_LIST_ITEM_SELECTED event from selected item? I really need this kind of behavior.
Thanks!
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by shawnee »

I bind this event:
this->mylist->Bind(wxEVT_LEFT_UP, &MyList::OnClickListItem, this, this->mylist->GetId());

void MyList::OnClickListItem(wxMouseEvent& event) {
int eventx = this->mylist->ScreenToClient(wxGetMousePosition()).x;
int eventy = this->mylist->ScreenToClient(wxGetMousePosition()).y;
wxPoint mouse(eventx, eventy);

int flag = wxLIST_HITTEST_ONITEMLABEL;
long row = this->mylist->HitTest(mouse, flag);
if (row >= 0) {
//do action here...
}
}

It seems solve the problem. But why I only can get double click(left) mouse event in OnClickListItem?
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by shawnee »

If I change wxEVT_LEFT_UP to wxEVT_LEFT_DOWN, then I could get left button single clicking mouse event.
But I got a new problem, no highlight on selected item is showing. If response wxEVT_LEFT_UP, the highlight is showing.
Anybody could help me out? Thanks!
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by shawnee »

even I set -- this->mylist->SetItemState(row, 1, wxLIST_STATE_SELECTED), it's still useless.
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by shawnee »

Oh, I got stupid, should be: this->mylist->SetItemState(row, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED) to select an item.
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by shawnee »

The whole solve way is:

this->mylist->Bind(wxEVT_LEFT_DOWN, &MyList::OnClickListItem, this, this->mylist->GetId());
this->mylist->Bind(wxEVT_LEFT_DCLICK, &MyList::OnClickListItem, this, this->mylist->GetId());

void MyList::OnClickListItem(wxMouseEvent& event) {
int eventx = this->mylist->ScreenToClient(wxGetMousePosition()).x;
int eventy = this->mylist->ScreenToClient(wxGetMousePosition()).y;
wxPoint mouse(eventx, eventy);

int flag = wxLIST_HITTEST_ONITEMLABEL;
long row = this->mylist->HitTest(mouse, flag);
if (row >= 0) {
//do action here...

this->mylist->SetItemState(row, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED)
}
}
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by doublemax »

Code: Select all

int eventx = this->mylist->ScreenToClient(wxGetMousePosition()).x;
int eventy = this->mylist->ScreenToClient(wxGetMousePosition()).y;
wxPoint mouse(eventx, eventy);
Just use event.GetPosition(), that is in client coordinates already.

Call event.Skip() in the mouse event handler, so that the default processing can take place. Then you don't have to do the selection yourself.
Use the source, Luke!
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by shawnee »

Thanks doublemax, you always give us final professional answers!
I'd like ask you another question. If the listctrl is under Extended mode, how can I know the shift or ctrl key is pressing down when select item in list?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by doublemax »

Code: Select all

event.ShiftDown()
https://docs.wxwidgets.org/trunk/classw ... state.html

If this does not work with a wxMouseEvent (although it should), use wxGetKeyState():
https://docs.wxwidgets.org/trunk/group_ ... 41b71ce84f
Use the source, Luke!
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: wxEVT_LIST_ITEM_SELECTED could not be triggered again if the item has been selected

Post by shawnee »

doublemax wrote:

Code: Select all

event.ShiftDown()
https://docs.wxwidgets.org/trunk/classw ... state.html

If this does not work with a wxMouseEvent (although it should), use wxGetKeyState():
https://docs.wxwidgets.org/trunk/group_ ... 41b71ce84f
event.ShiftDown() and event.ControlDown() are working. Thanks!
Post Reply