C++ 如何正确显示值为“0”的wxMessageBox;string01";?

C++ 如何正确显示值为“0”的wxMessageBox;string01";?,c++,wxwidgets,wxformbuilder,C++,Wxwidgets,Wxformbuilder,如何正确显示值为“string01”的MessageBox gui.h: /////////////////////////////////////////////////////////////////////////// // C++ code generated with wxFormBuilder (version Oct 26 2018) // http://www.wxformbuilder.org/ // // PLEASE DO *NOT* EDIT THIS FILE! ///

如何正确显示值为“string01”的MessageBox

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();

};
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
}
我试图在wxMessageBox中显示“string01”的值(如“inheritedgui.cpp”中所示),但是当我单击“m_button2”时,什么都没有发生,我不知道为什么:/


当我单击“m_button2”(显示“列表”)时,wxMessageBox会显示出来。

您在一个方法(
m_button1OnButtonClick
)中声明
std::string string01
),并尝试在另一个方法(
m_button2OnButtonClick
)中使用它

为了能够在您正在尝试的另一种方法中使用它,请将
std::string string01
设置为
class inheritedgui
上的成员变量

然后当你打电话时:

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

另外,特别需要使用
std::string
而不是
wxString
?如果没有,只需坚持使用
wxString

,您不需要将从文本控件接收的值转换为std::string,正如我在前面的问题中所说的那样

您还需要将此wxString声明为类的成员

为了修复编译错误:

wxMessageBox( string01, wxT("This is the title"), wxICON_INFORMATION );
此外,最好使用
Bind()
而不是
Connect()

在析构函数中,您不必
UnBind()
事件-它将自动发生

最后,您不必声明函数
virtual
,它应该可以在没有。事实上,wxWidgets中的事件处理程序很少应该是虚拟的


谢谢。

您是否添加了
std::string string01
类继承GUI的主体
?如果是这样,您不需要在
m_button1OnButtonClick
中再次声明它。只需使用
string01=m_textcrl1->GetValue().ToStdString()
做了一些更改(),现在代码构建得很好(
将std::string string01;
添加到
类inheritedgui
的主体中),但是单击“m_按钮2”后仍然没有发生任何事情。如果我将
std::string
更改为
wxString
,则会出现以下错误:`^/home/charlesdward/Documents/Workspace/FP_Clinic/inheritedgui.cpp:22:15:错误:对“wxString”构造函数的调用不明确wxMessageBox(wxString(string01.c_str(),wxConvUTF8),wxT(“这是标题”),wxICON\u信息)`代码是由wxFormBuilder自动生成的,如果我更改Connect以绑定,那么在以后再次使用wxFormBuilder时,更改将被还原。。。不过,感谢您的指示,当我学会手工编写gui时,我会记住这一点。我将您提到的关于wxString和wxMessageBox的更正改为inheritedgui.cpp:其余代码(gui.h、gui.cpp、inheritedgui.h)与第一篇文章中的相同。现在我没有得到编译错误,但是当我按下“m_按钮2”时什么也没有发生。希望你能帮我解决这个问题,因为我被卡住了。致以最良好的问候,感谢您的帮助我编辑了主要帖子,所以您不需要进入粘贴箱link@etcCoder,我编辑了我的答案。另外,我没有看到您的OP的编辑,因为我仍然看到
Connect()
在那里调用。另外,我很好奇-为什么您要从
mainlogdialogbase
继承一个类?为什么不把所有的东西都放在一个主类中呢?我试着将“connect”改为“bind”,但是我遇到了构建错误。以下是已编辑的代码和生成错误:。将“连接”更改为“绑定”会使wxMessageBox显示吗?
wxMessageBox( string01, wxT("This is the title"), wxICON_INFORMATION );
Bind( wxEVT_CLOSE_WINDOW, &inheritedGui::OnCloseDialog, this );
Bind( wxEVT_COMMAND_BUTTON_CLICKED, &inheritedGui::m_button1OnButtonClick, this );