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.
Hi, I'm trying to use "wxStdDialogButtonSizer", but It don't work like I expected. I use Code::Block with wxSmith to edit my GUI and I have added Save/Cancel buttons to my dialog. Only the Cancel button does something (close the dialog). "Save" button doesn't do anything.
I have try to add button by code instead of by wxSmith, But I don't think is the way to do it.
The following code work, but I don't see the buttons created in the wxSmith editor. I want to use the integreated buttons in the "wxStdDialogButtonSizer" to be able to see the look in wxSmith editor.
I was trying to create a button in "wxStdDialogButtonSizer" but you just need to create a handler for the button that is created with the "wxStdDialogButtonSizer". Like "ONEEYEMAN" said. By using the right "wxID". To maybe help someone else I have write all steps.
Just create "command event function" in ".h" file and implement it in ".cpp" file. After that we need to connect, in the constructor, the "button's id" with the "command event function" created earlier.
myWxDialog::myWxDialog(/*param*/){
//(*Initialize(ConfigurationDialog) //Auto generated code
//Some stuff
StdDialogButtonSizer = new wxStdDialogButtonSizer();
StdDialogButtonSizer->AddButton(new wxButton(this, wxID_CANCEL, wxEmptyString)); // the id for the cancel button is: wxID_CANCEL
StdDialogButtonSizer->AddButton(new wxButton(this, wxID_SAVE, wxEmptyString)); // the id for the save button is: wxID_SAVE
StdDialogButtonSizer->Realize();
//Some stuff
//*) //End - Auto generated code
//Connect button in "StdDialogButtonSizer"
Connect(wxID_SAVE,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&myWxDialog::OnButSaveClick);
Connect(wxID_CANCEL,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&myWxDialog::OnButCancelClick);
}
void myWxDialog::OnButSaveClick(wxCommandEvent& event){
// Save stuff
wxMessageBox("You have click on save button. myWxDialog::OnButSaveClick");
this->Close();
}
void myWxDialog::OnButCancelClick(wxCommandEvent& event){
wxMessageBox("myWxDialog::OnButCancelClick");
this->EndModal(0);
}
wxFAIL_MSG() will show a message box if in debug mode, does nothing in release. If you click Stop it will break into your code at the wxFAIL_MSG else continues execution
If opening a dialog with ShowModal() then you must call EndModal() to close it.
CPP File
myWxDialog::myWxDialog(/*param*/){
//(*Initialize(ConfigurationDialog) //Auto generated code
//Some stuff
StdDialogButtonSizer = new wxStdDialogButtonSizer();
StdDialogButtonSizer->AddButton(new wxButton(this, wxID_CANCEL)); // the id for the cancel button is: wxID_CANCEL
StdDialogButtonSizer->AddButton(new wxButton(this, wxID_SAVE)); // the id for the save button is: wxID_SAVE
StdDialogButtonSizer->Realize();
//Some stuff
//*) //End - Auto generated code
// Use Bind() and then you do not need event tables and can dynamically bind/unbind events which is very useful
Bind(wxEVT_BUTTON, &myWxDialog::OnButtonClick, this);
}
// In this case, the Unbind() is not necessary, I include for completeness of the example
myWxDialog::~myWxDialog()
{
// Exactly the same arguments as the Bind() call
Unind(wxEVT_BUTTON, &myWxDialog::OnButtonClick, this);
}
void OnButtonClick( wxCommandEvent& event)
{
// The dialog will handle the button click return value, we just need to do local stuff then event.Skip()
auto do_close{false};
switch( event.GetId() )
{
case wxID_SAVE:
do_close = HandleButtonSave();
break;
case wxID_CANCEL:
do_close = HandleButtonCancel();
break;
default: wxFAIL_MSG("Unhandled event Id");
}
// All button clicks lead to dialog closure on success, EndModal() here will return the wxID from this event to the
// caller of ShowModal()
if( do_close )
{
EndModal();
}
event.Skip(); // Pass to next handler in the chain
}
bool myWxDialog::HandleButtonSave(/*wxCommandEvent& event*/){
auto stuff_saved{false};
// Save stuff, and set stuff_saved true on success
if( stuff_saved)
{
// Show success message
}
else
{
// Show fail message
}
return stuff_saved;
}
bool myWxDialog::OnButCancelClick(wxCommandEvent& event){
return ( wxMessageBox("Close this dialog?", "Confirm", wxYES_NO, this) == wxYES);
}