How to set focus on message box button.

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
rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

How to set focus on message box button.

Post by rafae11 » Wed Jul 18, 2018 1:58 pm

I am having difficulty finding the command to setfocus on the OK button in the messagebox.
I would like to know how to close the message box automatically by either simulating a button click on the "OK" button or the x at the top right corner.

the code below is the one i am using to generate a message box.
wxString msg;
msg.Printf(wxT("Calibration Loaded"));
msg << "\nChecksum Valid";
wxMessageBox(msg, wxT("Calibration"), wxOK | wxICON_INFORMATION , this);

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

Re: How to set focus on message box button.

Post by ONEEYEMAN » Wed Jul 18, 2018 3:21 pm

Hi,
You don't have to set the focus on the button. It will be focused automatically.
Do you have any issue closing the message box by pressing Enter?

Thank you.

rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

Re: How to set focus on message box button.

Post by rafae11 » Wed Jul 18, 2018 5:04 pm

the section of code that gets called is part of a test suite which i need to run automatically. the test stops at the point where the message box is created, after i close it manually the test carries on.

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

Re: How to set focus on message box button.

Post by ONEEYEMAN » Wed Jul 18, 2018 5:09 pm

Hi,
So you want to close message box automatically?
Did you look at how wxWidgets wrote their testing suite?

Thank you.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to set focus on message box button.

Post by PB » Wed Jul 18, 2018 5:22 pm

wxMessageBox() will display a modal wxMessageDialog (which at least on MSW can be a native OS-provided dialog).

I may be wrong but: How would you expect to do anything that affects that dialog: there is no dialog before you call wxMessageBox() and the function won't return till after the dialog was closed?

I believe you will have to find another solution, perhaps unfortunately an intrusive one (i.e., behave differently when a test is run)...

rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

Re: How to set focus on message box button.

Post by rafae11 » Wed Jul 18, 2018 6:14 pm

I found something that might work wxModalDialogHook.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to set focus on message box button.

Post by PB » Wed Jul 18, 2018 6:28 pm

Actually it seems I was wrong. I can test only on MSW but wxUIActionSimulator seems to work with wxMessageBox:

Code: Select all

#include <wx/wx.h>
#include <wx/uiaction.h>

class MyFrame : public wxFrame
{
public:
    MyFrame() : wxFrame(NULL, wxID_ANY, "Test", wxDefaultPosition, wxSize(600, 400))
    {
        wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);
        
        wxButton* button;
        
        button = new wxButton(this, wxID_ANY, "Test OK");
        bSizer->Add(button, 0, wxEXPAND | wxALL, 5);
        button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnTestOK, this);

        button = new wxButton(this, wxID_ANY, "Test Cancel");
        bSizer->Add(button, 0, wxEXPAND | wxALL, 5);
        button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnTestCancel, this);

        wxTextCtrl* textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, 
            wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);        
        bSizer->Add(textCtrl, 1, wxEXPAND | wxALL , 5);
        wxLog::SetActiveTarget(new wxLogTextCtrl(textCtrl));
        SetSizer(bSizer);
    }
private:    
    void OnTestOK(wxCommandEvent&) { Test(true); }
    void OnTestCancel(wxCommandEvent&) { Test(false); }
    
    void Test(bool testOK)
    {
        wxUIActionSimulator sim;                
        wxString result;
        int style = wxOK | wxCANCEL | wxICON_INFORMATION;

        if ( testOK )
            style |= wxOK_DEFAULT;
        else
            style |= wxCANCEL_DEFAULT;

        sim.Char(WXK_RETURN);
                
        switch ( wxMessageBox("Testing", wxT("Calibration"),  style, this) )
        {
            case wxOK: result = "OK"; break;
            case wxCANCEL: result = "Cancel"; break;
            default: result = "Unknown return code";
        }
        wxLogMessage("Testing '%s', returned '%s'", testOK ? "OK" : "Cancel", result);        
    }
};

class MyApp : public wxApp
{
public:
    bool OnInit()
    {
        (new MyFrame())->Show();
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);
Not sure how reliable (or portable) it is...

rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

Re: How to set focus on message box button.

Post by rafae11 » Thu Jul 19, 2018 11:36 am

this works perfect for what i need. http://docs.wxwidgets.org/3.1/classwx_m ... _hook.html
I only need to add it to the test application and the code for my main application stays the same.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to set focus on message box button.

Post by PB » Thu Jul 19, 2018 12:00 pm

Just curious: how would you access the instance of the dialog displayed by wxMessageBox()?

rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

Re: How to set focus on message box button.

Post by rafae11 » Thu Jul 19, 2018 12:39 pm

I just copied the example code and modified the section shown below and when the test runs it automatically closes the message box.

Code: Select all

    virtual int Enter(wxDialog* dialog)
    {
       return wxID_OK;
    }
my original plan was to set focus and simulate a key press same as what you have shown in your @PB example, the issue i have with it is i need to modify the application code.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to set focus on message box button.

Post by PB » Thu Jul 19, 2018 2:22 pm

I see, thanks.

Post Reply