wxButton simulate click 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
Nick
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Apr 11, 2019 12:50 am

wxButton simulate click

Post by Nick » Sat Apr 13, 2019 5:12 am

I have 1 Button that executes a command when clicked. Clicking on the button works fine!
Var name = BtCancelar
Identifier = ID_BtCancelar

Code: Select all

void Main::OnBtCancelarClick(wxCommandEvent& event) {
   PnEditar->Hide();
   PnGrid->Show();
}
I'm trying to run the Click event through code but I can not do. Simulate a click via code

Code: Select all

BtCancelar->Click();
Main::OnBtCancelarClick();
OnBtCancelarClick();
BtCancelar->Command(OnBtCancelarClick());
BtCancelar->Command(OnBtCancelarClick);
BtCancelar->Command();
BtCancelar->OnBtCancelarClick;

User avatar
doublemax
Moderator
Moderator
Posts: 13571
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxButton simulate click

Post by doublemax » Sat Apr 13, 2019 7:34 am

Calling an event handler directly is generally bad practice. Instead put the actual code in a separate method:

Code: Select all

void Main::DoCancel() {
   PnEditar->Hide();
   PnGrid->Show();
}

void Main::OnBtCancelarClick(wxCommandEvent& event) {
   DoCancel();
}
Now you can just call DoCancel().

If you want to call it from another method of "Main", you can just use DoCancel() to call it. If you want to call it from another class you need a pointer to the "Main" instance. How to get that depends on the context. In that case you need to provide more information.
Use the source, Luke!

Nick
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Apr 11, 2019 12:50 am

Re: wxButton simulate click

Post by Nick » Sat Apr 13, 2019 8:34 am

doublemax wrote:
Sat Apr 13, 2019 7:34 am
Calling an event handler directly is generally bad practice. Instead put the actual code in a separate method:

Code: Select all

void Main::DoCancel() {
   PnEditar->Hide();
   PnGrid->Show();
}

void Main::OnBtCancelarClick(wxCommandEvent& event) {
   DoCancel();
}
Now you can just call DoCancel().

If you want to call it from another method of "Main", you can just use DoCancel() to call it. If you want to call it from another class you need a pointer to the "Main" instance. How to get that depends on the context. In that case you need to provide more information.
Thanks for answering!
I had already done it that way. My Source has some functions.
Thanks also for informing you that it is bad practice to do this! Well, I did not know that!

But I would still really like to know how I can run the event by Code.
Because my functions work normally, accessing the controls, and can be executed from anywhere.
What I understood in the case of events is that it seems to be missing to place the arguments necessary to call their function between the parentheses.
Knowing that to me is important because it will give me more knowledge of how these functions.

I'm currently using Codeblocks with wxSmith, creating the Forms. But it's not my ultimate goal. I'm studying for wxSmith to understand how he writes the Code.
Currently I already know what I can modify, and how to create some widgets. Because when I learn better, my idea would be just to use wxSmith to model a Form. Because the rest I would actually write in hand, because I do not like the way he writes, mark the code, I did not even like the Codeblocks IDE.

I usually like Code with as few lines as possible. I do not like splitting files, I've never liked a Code with 20 lines divided into 2, 4 or 6 files as many people do. Which is why I really want to know how everything works!

User avatar
doublemax
Moderator
Moderator
Posts: 13571
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxButton simulate click

Post by doublemax » Sat Apr 13, 2019 9:11 am

The event handler needs a wxCommandEvent as parameter. Theoretically you could call it like this:

Code: Select all

wxCommandEvent evt;
OnBtCancelarClick( evt );
Why it is bad practice? The wxCommandEvent normally contains lots of information, e.g. a pointer to the object that was clicked, its ID, mouse coordinates, status of keyboard modifiers, etc. But all this information is not set when you create an empty wxCommandEvent like this. And depending on which of these information the event handler actually uses, it may or may not work correctly.

IOW: Don't do this.
Use the source, Luke!

Nick
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Apr 11, 2019 12:50 am

Re: wxButton simulate click

Post by Nick » Sat Apr 13, 2019 10:18 am

doublemax wrote:
Sat Apr 13, 2019 9:11 am
The event handler needs a wxCommandEvent as parameter. Theoretically you could call it like this:

Code: Select all

wxCommandEvent evt;
OnBtCancelarClick( evt );
Why it is bad practice? The wxCommandEvent normally contains lots of information, e.g. a pointer to the object that was clicked, its ID, mouse coordinates, status of keyboard modifiers, etc. But all this information is not set when you create an empty wxCommandEvent like this. And depending on which of these information the event handler actually uses, it may or may not work correctly.

IOW: Don't do this.
Thanks again for the details in the explanation. I was able to run the event. Worked in 2 ways below. At least I was happy to know how to run.

Code: Select all

wxCommandEvent evt;
OnBtPesquisarClick(evt);
wxCommandEvent wxEVT_COMMAND_BUTTON_CLICKED;
OnBtPesquisarClick(wxEVT_COMMAND_BUTTON_CLICKED);
Anyway, I'm following your advice. But I know I need to study more about how these events work later. Thank you!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3259
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxButton simulate click

Post by ONEEYEMAN » Sat Apr 13, 2019 11:43 pm

Hi,
In short - don't do this. Ever!!! Unless you plan to work on wxWIdgets in the future.

In long explanation - there is only 0.000001 % of use cases where you need to generate the event yourself and send it for processing in the user code. One of those cases is to write the testing framework for your application. But even in this case the approach suggested by doublemax is better. Remember you absolutely need to know the internals of the wxWidgets in order to successfully produce such a code so that it will work correctly in all cases.

So all in all - there is no good reason for you to do it like this.

Thank you.

Post Reply