如何获取在 wxTextCtrl 中输入的文本并将其分配给字符串变量?
How to get text entered in wxTextCtrl and assign it to a string variable?
我在 wxFormBuilder 中创建了以下 GUI:
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
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/// Class MainDialogBase
///////////////////////////////////////////////////////////////////////////////
class MainDialogBase : public wxDialog
{
private:
protected:
wxStaticText* m_staticText3;
wxStaticText* m_staticText1;
wxTextCtrl* m_textCtrl1;
wxStaticText* m_staticText4;
wxTextCtrl* m_textCtrl2;
wxStaticText* m_staticText6;
wxStaticText* m_staticText5;
wxTextCtrl* m_textCtrl3;
wxStaticText* m_staticText7;
wxTextCtrl* m_textCtrl4;
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(); }
public:
MainDialogBase( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 400,300 ), 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 );
wxBoxSizer* bSizer5;
bSizer5 = new wxBoxSizer( wxVERTICAL );
m_staticText6 = new wxStaticText( this, wxID_ANY, _("Doctor's data"), wxDefaultPosition, wxSize( 105,-1 ), 0 );
m_staticText6->Wrap( -1 );
m_staticText6->SetFont( wxFont( 10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxT("Sans") ) );
bSizer5->Add( m_staticText6, 0, wxALL, 5 );
wxBoxSizer* bSizer6;
bSizer6 = new wxBoxSizer( wxHORIZONTAL );
m_staticText5 = new wxStaticText( this, wxID_ANY, _("First Name:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText5->Wrap( -1 );
bSizer6->Add( m_staticText5, 0, wxALL, 5 );
m_textCtrl3 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
bSizer6->Add( m_textCtrl3, 0, wxALL, 5 );
bSizer5->Add( bSizer6, 1, wxEXPAND, 5 );
wxBoxSizer* bSizer7;
bSizer7 = new wxBoxSizer( wxHORIZONTAL );
m_staticText7 = new wxStaticText( this, wxID_ANY, _("Last Name:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText7->Wrap( -1 );
bSizer7->Add( m_staticText7, 0, wxALL, 5 );
m_textCtrl4 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
bSizer7->Add( m_textCtrl4, 0, wxALL, 5 );
bSizer5->Add( bSizer7, 1, wxEXPAND, 5 );
bSizer8->Add( bSizer5, 1, 0, 5 );
mainSizer->Add( bSizer8, 1, wxEXPAND, 5 );
mainSizer->Add( 0, 0, 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 ) );
}
MainDialogBase::~MainDialogBase()
{
// Disconnect Events
this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( MainDialogBase::OnCloseDialog ) );
}
我还继承了以下class:
继承gui.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 );
public:
/** Constructor */
inheritedgui( wxWindow* parent );
//// end generated class members
};
#endif // __inheritedgui__
继承gui.cpp:
#include "inheritedgui.h"
inheritedgui::inheritedgui( wxWindow* parent )
:
MainDialogBase( parent )
{
}
void inheritedgui::OnCloseDialog( wxCloseEvent& event )
{
// TODO: Implement OnCloseDialog
}
你能告诉我如何在 m_textCtrl1
中输入文本并在按下 m_button1
时将其分配给字符串变量吗?
我正在尝试将一个简单的程序从 JavaFX 移植到 C++,该程序主要接受用户输入(来自 wxTextCtrl
控件)并将输入分配给字符串变量,这样我就可以显示存储的值在按下 m_button2
时消息框的变量中。
我一直在做一些 google 搜索,但 C++ 对我来说有点困难。
m_textCtrl1
是一个 wxTextCtrl
, which has a GetValue()
method that returns a wxString
, which in turn has a ToStdString()
用于转换为 std::string
:
std::string s = m_textCtrl1->GetValue().ToStdString();
@etcCoder,
如果平移以使用 wxWidgets 中的消息框函数,则不需要将字符串转换为 std::string - wxMessageBox() 接受 wxString。
谢谢。
我在 wxFormBuilder 中创建了以下 GUI:
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
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/// Class MainDialogBase
///////////////////////////////////////////////////////////////////////////////
class MainDialogBase : public wxDialog
{
private:
protected:
wxStaticText* m_staticText3;
wxStaticText* m_staticText1;
wxTextCtrl* m_textCtrl1;
wxStaticText* m_staticText4;
wxTextCtrl* m_textCtrl2;
wxStaticText* m_staticText6;
wxStaticText* m_staticText5;
wxTextCtrl* m_textCtrl3;
wxStaticText* m_staticText7;
wxTextCtrl* m_textCtrl4;
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(); }
public:
MainDialogBase( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 400,300 ), 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 );
wxBoxSizer* bSizer5;
bSizer5 = new wxBoxSizer( wxVERTICAL );
m_staticText6 = new wxStaticText( this, wxID_ANY, _("Doctor's data"), wxDefaultPosition, wxSize( 105,-1 ), 0 );
m_staticText6->Wrap( -1 );
m_staticText6->SetFont( wxFont( 10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxT("Sans") ) );
bSizer5->Add( m_staticText6, 0, wxALL, 5 );
wxBoxSizer* bSizer6;
bSizer6 = new wxBoxSizer( wxHORIZONTAL );
m_staticText5 = new wxStaticText( this, wxID_ANY, _("First Name:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText5->Wrap( -1 );
bSizer6->Add( m_staticText5, 0, wxALL, 5 );
m_textCtrl3 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
bSizer6->Add( m_textCtrl3, 0, wxALL, 5 );
bSizer5->Add( bSizer6, 1, wxEXPAND, 5 );
wxBoxSizer* bSizer7;
bSizer7 = new wxBoxSizer( wxHORIZONTAL );
m_staticText7 = new wxStaticText( this, wxID_ANY, _("Last Name:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText7->Wrap( -1 );
bSizer7->Add( m_staticText7, 0, wxALL, 5 );
m_textCtrl4 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
bSizer7->Add( m_textCtrl4, 0, wxALL, 5 );
bSizer5->Add( bSizer7, 1, wxEXPAND, 5 );
bSizer8->Add( bSizer5, 1, 0, 5 );
mainSizer->Add( bSizer8, 1, wxEXPAND, 5 );
mainSizer->Add( 0, 0, 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 ) );
}
MainDialogBase::~MainDialogBase()
{
// Disconnect Events
this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( MainDialogBase::OnCloseDialog ) );
}
我还继承了以下class:
继承gui.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 );
public:
/** Constructor */
inheritedgui( wxWindow* parent );
//// end generated class members
};
#endif // __inheritedgui__
继承gui.cpp:
#include "inheritedgui.h"
inheritedgui::inheritedgui( wxWindow* parent )
:
MainDialogBase( parent )
{
}
void inheritedgui::OnCloseDialog( wxCloseEvent& event )
{
// TODO: Implement OnCloseDialog
}
你能告诉我如何在 m_textCtrl1
中输入文本并在按下 m_button1
时将其分配给字符串变量吗?
我正在尝试将一个简单的程序从 JavaFX 移植到 C++,该程序主要接受用户输入(来自 wxTextCtrl
控件)并将输入分配给字符串变量,这样我就可以显示存储的值在按下 m_button2
时消息框的变量中。
我一直在做一些 google 搜索,但 C++ 对我来说有点困难。
m_textCtrl1
是一个 wxTextCtrl
, which has a GetValue()
method that returns a wxString
, which in turn has a ToStdString()
用于转换为 std::string
:
std::string s = m_textCtrl1->GetValue().ToStdString();
@etcCoder,
如果平移以使用 wxWidgets 中的消息框函数,则不需要将字符串转换为 std::string - wxMessageBox() 接受 wxString。
谢谢。