Left Down in button 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
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sun May 05, 2019 7:12 am

Left Down in button

Post by mxoliveira73 »

resarching in foruns here, I learn handle the left button down event, this form:

Code: Select all

BEGIN_EVENT_TABLE(MainFrame, wxFrame)	
	EVT_BUTTON(ID_Of_Action_Object, MainFrame :: CalledMethod)
END_EVENT_TABLE();

btn = new wxButton(this, BUTTON_ID, "Button", wxDefaultPosition, wxDefaultSize);
btn->Connect(BUTTON_ID, wxEVT_LEFT_DOWN, wxCommandEventHandler(MyFrame::OnDown));


void MainFrame :: OnDown(wxCommandEvent& event){

   //do something after clicking on the button;
};
Ok...

It works if // do something is: wxMessageBox("lalala"), for example

but,

if the action is btn-SetLabel("lalala"); or other action origin in object ___ btn->SetBackgroundColour...
doesn't work. Compile is normal, but in click the button is generate a fatal error...

Thanks if someone could help me...
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Left Down in button

Post by doublemax »

Replace

Code: Select all

btn->Connect(BUTTON_ID, wxEVT_LEFT_DOWN, wxCommandEventHandler(MyFrame::OnDown));
with

Code: Select all

btn->Connect(BUTTON_ID, wxEVT_LEFT_DOWN, wxCommandEventHandler(MyFrame::OnDown), NULL, this);
or

Code: Select all

Connect(BUTTON_ID, wxEVT_LEFT_DOWN, wxCommandEventHandler(MyFrame::OnDown));
It's also better to use Bind() instead of Connect(), as it would create an error at compile time.
Use the source, Luke!
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sun May 05, 2019 7:12 am

Re: Left Down in button

Post by mxoliveira73 »

Ok, lets go to Bind. I'm studying hard. Consulting the documentation event.h I found:
void Bind(const EventTag &eventType, void (Class::*method)(EventArg &), EventHandler *handler, int winid = wxID_ANY,int lastId = wxID_ANY,
wxObject *userData = NULL)

Then, i could make this:
btn->Bind( wxEVT_LEFT_DOWN,(MyFrame::OnDown), wxCommandEventHandler, ID_btn1, wxID_ANY, NULL);

is it?
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Left Down in button

Post by doublemax »

I can't test this right now, from the top of my head, try this:

Code: Select all

btn->Bind( wxEVT_LEFT_DOWN, &MyFrame::OnDown, NULL, this );
If it doesn't work, continue with one of my Connect solutions for now, they should work.
Use the source, Luke!
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sun May 05, 2019 7:12 am

Re: Left Down in button

Post by mxoliveira73 »

Doesn't work. Where can i search and study. Tips or links please...
I really don't understand, maybe the wrong it's mine, but 3 examples on this page
https://docs.wxwidgets.org/3.0/overview_events.html works fine with wxEVT_COMMAND_MENU_SELECTED etc.. but adapt to wxEvt_LEFT_DOWN doesn't work.... what pags, links do you recommend, please....
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Left Down in button

Post by doublemax »

What exactly does not work? Do you get a compile error (if yes, which one), or does it compile, but the event handler doesn't get called?
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7481
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Left Down in button

Post by ONEEYEMAN »

Hi,
Also curious - why do you need to handle low-level event, when the EVT_BUTTON() exist?

Thank you.
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sun May 05, 2019 7:12 am

Re: Left Down in button

Post by mxoliveira73 »

******************method 1 ************************************************************************
void OnDown(wxCommandEvent & event);
btn->Bind( wxEVT_LEFT_DOWN, &MyFrame::OnDown, NULL, this );
void MyFrame::OnDown(wxCommandEvent & event)
{
muteChannel1();
btn->SetBackgroundColour(*wxRED);
}

ERROR ON COMPILE:
error: no matching function for call to 'wxButton::Bind(const wxEventTypeTag<wxMouseEvent>&, void (MyFrame::*)(wxCommandEvent&), NULL, MyFrame*)
********************************************************************************************************
Last edited by mxoliveira73 on Tue Jun 04, 2019 1:49 am, edited 1 time in total.
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sun May 05, 2019 7:12 am

Re: Left Down in button

Post by mxoliveira73 »

For oneeyeman,

I don't now if I can explain... but is that.

Evt_Button is a little bit diferent: if you click in the button and hold, the action don't happens; it only happens when you release the button(left mouse).

using left_down in the exact moment that you down the button left mouse, the action happens....

the feel is of more velocity in application...

i have success in this objective, using Connect, whit help of doublemax...
Now, i need use Bind, because it's more modern, flexible and previne errors and future problems...

Ok, I'm not expert in WxWidgets...

Im using examples of Bind (using wxEVT_LEFT_DOWN) and have no success; limit is mine...

sorry for the confusion of words, it's a little bit difficult to me explain in other language... ok?
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Left Down in button

Post by doublemax »

Try this:

Code: Select all

void OnDown(wxMouseEvent & event);

btn->Bind( wxEVT_LEFT_DOWN, &MyFrame::OnDown, this );
void MyFrame::OnDown(wxMouseEvent & event)
{
 muteChannel1();
 btn->SetBackgroundColour(*wxRED);
}
Use the source, Luke!
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sun May 05, 2019 7:12 am

Re: Left Down in button

Post by mxoliveira73 »

wxWidgets-3.1.2\include\wx\event.h|374|error: no matching function for call to 'wxEventFunctorMethod<wxEventTypeTag<wxMouseEvent>, MyFrame, wxCommandEvent, MyFrame>::CheckHandlerArgument(wxEventFunctorMethod<wxEventTypeTag<wxMouseEvent>, MyFrame, wxCommandEvent, MyFrame>::EventClass*)'|

event.h line 374:
CheckHandlerArgument(static_cast<EventClass *>(NULL));
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Left Down in button

Post by doublemax »

I tested the last piece of code i posted and it should have worked. Can you show more code in context?
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Left Down in button

Post by PB »

It seems that mxoliveira73 used wrong handler declaration in OnDown, using wxCommandEvent instead of wxMouseEvent as a parameter
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Left Down in button

Post by doublemax »

PB wrote: Tue Jun 04, 2019 1:00 pm It seems that mxoliveira73 used wrong handler declaration in OnDown, using wxCommandEvent instead of wxMouseEvent as a parameter
Thanks, i missed that.
Use the source, Luke!
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sun May 05, 2019 7:12 am

Re: Left Down in button

Post by mxoliveira73 »

It seems that yoy are absolutelly right!!!!!!!!!!!!!!!!

Works fine now....

Thanks a lot...
Post Reply