How to correctly show a wxMessageBox with the value of "string01"?

1.3k views Asked by At

How to correctly show a MessageBox with the value of "string01"?

gui.h:

///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Oct 26 2018)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////

#pragma once

#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/intl.h>
#include <wx/string.h>
#include <wx/stattext.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/textctrl.h>
#include <wx/sizer.h>
#include <wx/statline.h>
#include <wx/bitmap.h>
#include <wx/image.h>
#include <wx/icon.h>
#include <wx/button.h>
#include <wx/dialog.h>
#include <wx/msgdlg.h> 

///////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
/// Class MainDialogBase
///////////////////////////////////////////////////////////////////////////////
class MainDialogBase : public wxDialog
{
    private:

    protected:
        wxStaticText* m_staticText3;
        wxStaticText* m_staticText1;
        wxTextCtrl* m_textCtrl1;
        wxStaticText* m_staticText4;
        wxTextCtrl* m_textCtrl2;
        wxStaticLine* m_staticLine;
        wxButton* m_button1;
        wxButton* m_button2;

        // Virtual event handlers, overide them in your derived class
        virtual void OnCloseDialog( wxCloseEvent& event ) { event.Skip(); }
        virtual void m_button1OnButtonClick( wxCommandEvent& event ) { event.Skip(); }
        virtual void m_button2OnButtonClick( wxCommandEvent& event ) { event.Skip(); }


    public:

        MainDialogBase( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size =     wxSize( 191,252 ), long style = wxCLOSE_BOX|wxDEFAULT_DIALOG_STYLE );
        ~MainDialogBase();

};

gui.cpp:

///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Oct 26 2018)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////

#include "gui.h"

///////////////////////////////////////////////////////////////////////////

MainDialogBase::MainDialogBase( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, pos, size, style )
{
    this->SetSizeHints( wxSize( -1,-1 ), wxDefaultSize );

    wxBoxSizer* mainSizer;
    mainSizer = new wxBoxSizer( wxVERTICAL );

    wxBoxSizer* bSizer8;
    bSizer8 = new wxBoxSizer( wxHORIZONTAL );

    wxBoxSizer* bSizer2;
    bSizer2 = new wxBoxSizer( wxVERTICAL );

    m_staticText3 = new wxStaticText( this, wxID_ANY, _("Patient's data"), wxDefaultPosition, wxSize( 105,-1 ), 0 );
    m_staticText3->Wrap( -1 );
    m_staticText3->SetFont( wxFont( 10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxT("Sans") ) );

    bSizer2->Add( m_staticText3, 0, wxALL, 5 );

    wxBoxSizer* bSizer3;
    bSizer3 = new wxBoxSizer( wxHORIZONTAL );

    m_staticText1 = new wxStaticText( this, wxID_ANY, _("First Name:"), wxDefaultPosition, wxDefaultSize, 0 );
    m_staticText1->Wrap( -1 );
    bSizer3->Add( m_staticText1, 0, wxALL, 5 );

    m_textCtrl1 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    bSizer3->Add( m_textCtrl1, 0, wxALL, 5 );


    bSizer2->Add( bSizer3, 1, wxEXPAND, 5 );

    wxBoxSizer* bSizer4;
    bSizer4 = new wxBoxSizer( wxHORIZONTAL );

    m_staticText4 = new wxStaticText( this, wxID_ANY, _("Last Name:"), wxDefaultPosition, wxDefaultSize, 0 );
    m_staticText4->Wrap( -1 );
    bSizer4->Add( m_staticText4, 0, wxALL, 5 );

    m_textCtrl2 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    bSizer4->Add( m_textCtrl2, 0, wxALL, 5 );


    bSizer2->Add( bSizer4, 1, wxEXPAND, 5 );


    bSizer8->Add( bSizer2, 1, 0, 5 );


    mainSizer->Add( bSizer8, 1, wxEXPAND, 5 );

    m_staticLine = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    mainSizer->Add( m_staticLine, 0, wxEXPAND | wxALL, 5 );

    m_button1 = new wxButton( this, wxID_ANY, _("Record Data"), wxDefaultPosition, wxDefaultSize, 0 );
    mainSizer->Add( m_button1, 0, wxALL|wxEXPAND, 5 );

    m_button2 = new wxButton( this, wxID_ANY, _("List"), wxDefaultPosition, wxDefaultSize, 0 );
    mainSizer->Add( m_button2, 0, wxALL|wxEXPAND, 5 );

    wxBoxSizer* bSizer9;
    bSizer9 = new wxBoxSizer( wxHORIZONTAL );


    mainSizer->Add( bSizer9, 1, wxEXPAND, 5 );


    this->SetSizer( mainSizer );
    this->Layout();

    this->Centre( wxBOTH );

    // Connect Events
    this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( MainDialogBase::OnCloseDialog ) );
    m_button1->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainDialogBase::m_button1OnButtonClick ), NULL, this );
    m_button2->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainDialogBase::m_button2OnButtonClick ), NULL, this );
}

MainDialogBase::~MainDialogBase()
{
    // Disconnect Events
    this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( MainDialogBase::OnCloseDialog ) );
    m_button1->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainDialogBase::m_button1OnButtonClick ), NULL, this );
    m_button2->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainDialogBase::m_button2OnButtonClick ), NULL, this );

}

inheritedgui.h:

#ifndef __inheritedgui__
#define __inheritedgui__

/**
@file
Subclass of MainDialogBase, which is generated by wxFormBuilder.
*/

#include "gui.h"

//// end generated include

/** Implementing MainDialogBase */
class inheritedgui : public MainDialogBase
{
    protected:
        // Handlers for MainDialogBase events.
        void OnCloseDialog( wxCloseEvent& event );
        void m_button1OnButtonClick(wxCommandEvent& event) override;
        void m_button2OnButtonClick(wxCommandEvent& event) override;
    public:
        /** Constructor */
        inheritedgui( wxWindow* parent );
    //// end generated class members

};

#endif // __inheritedgui__

inheritedgui.cpp:

#include "inheritedgui.h"

inheritedgui::inheritedgui( wxWindow* parent )
:
MainDialogBase( parent )
{   

}
wxString string01;

void inheritedgui::m_button1OnButtonClick(wxCommandEvent& event)
{

string01 = m_textCtrl1->GetValue().ToStdString();

}

void inheritedgui::m_button2OnButtonClick(wxCommandEvent& event)
{

wxMessageBox( string01, wxT("This is the title"), wxICON_INFORMATION );

}

void inheritedgui::OnCloseDialog( wxCloseEvent& event )
{
// TODO: Implement OnCloseDialog
}

I'm trying to show the value of "string01" in a wxMessageBox (as seen in "inheritedgui.cpp"), however when I click "m_button2", nothing happens and I don't know why :/

https://i.postimg.cc/jd6WDc6z/Screenshot-20200227-040106.png

What changes do I need to do so the wxMessageBox shows up when I click "m_button2" (is the one which says "List")?

2

There are 2 answers

2
André Caceres On

You are declaring std::string string01 in one method (m_button1OnButtonClick) and trying to use it in another one (m_button2OnButtonClick).

To be able to use it in another method like you are trying, make std::string string01 a member variable on class inheritedgui.

Then when you call:

wxMessageBox(wxString(string01.c_str(), wxConvUTF8), wxT("This is the title"), wxICON_INFORMATION);

Also, there is a particular need to use std::string instead of wxString? If not, just stick to wxString.

7
Igor On

You don't need to convert the value you receive from the text control to std::string, as I said in the previous question.

You also need to declare this wxString as a member of the class.

And in order to fix the compilation error:

wxMessageBox( string01, wxT("This is the title"), wxICON_INFORMATION );

Also, it is preferred to use Bind() instead of Connect():

Bind( wxEVT_CLOSE_WINDOW, &inheritedGui::OnCloseDialog, this );
Bind( wxEVT_COMMAND_BUTTON_CLICKED, &inheritedGui::m_button1OnButtonClick, this );

In the destructor you don't have to UnBind() the event - it will happen automatically.

And finally - you don't have to declare the function virtual - it should work without. In fact event handlers in wxWidgets very rarely should be virtual.

Thank you.