注意:我完全修改了這個問題,並將其轉換爲專門針對此問題的示例項目,因此Nicks的答案不再有意義。 wxQuestionMain.h和wxQuestionMain.cpp是輕微修改的wxWidget文件,由Code :: Blocks自動生成。WxWidgets - 從主文件以外的文件更改texbox
當我點擊「開始」按鈕,我想在「wxQuestionMain.cpp」按鈕事件稱之爲「somefunction()」,這是裏面的「otherFile.cpp」。這工作得很好。但是我想從「somefunction()」中改變文本框「txtCtrl1」中的文本,並且不會工作,因爲「somefunction()」不是wxWidget類的一部分,我不希望它成爲。 wxwidget類是在「wxQuestionMain.h」中創建的。
wxQuestionMain.h - >剛剛創建類
#ifndef WXQUESTIONMAIN_H
#define WXQUESTIONMAIN_H
#define BOOST_FILESYSTEM_VERSION 2
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "wxQuestionApp.h"
#include <wx/button.h>
#include <wx/statline.h>
class wxQuestionDialog: public wxDialog
{
public:
wxQuestionDialog(wxDialog *dlg, const wxString& title);
~wxQuestionDialog();
protected:
enum
{
idBtnGo = 1000
};
wxStaticText* m_staticText1;
wxStaticLine* m_staticline1;
wxButton* BtnGo;
wxTextCtrl* textCtrl1;
private:
void OnClose(wxCloseEvent& event);
void OnGo(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
void somefunction();
#endif // WXQUESTIONMAIN_H
wxQuestionMain.cpp - >大量的內容非常重要的,然後在最底層,處理ButtonClick事件的功能。
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "wxQuestionMain.h"
//helper functions
enum wxbuildinfoformat {
short_f, long_f };
wxString wxbuildinfo(wxbuildinfoformat format)
{
wxString wxbuild(wxVERSION_STRING);
if (format == long_f)
{
#if defined(__WXMSW__)
wxbuild << _T("-Windows");
#elif defined(__WXMAC__)
wxbuild << _T("-Mac");
#elif defined(__UNIX__)
wxbuild << _T("-Linux");
#endif
#if wxUSE_UNICODE
wxbuild << _T("-Unicode build");
#else
wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
}
return wxbuild;
}
BEGIN_EVENT_TABLE(wxQuestionDialog, wxDialog)
EVT_CLOSE(wxQuestionDialog::OnClose)
EVT_BUTTON(idBtnGo, wxQuestionDialog::OnGo)
END_EVENT_TABLE()
wxQuestionDialog::wxQuestionDialog(wxDialog *dlg, const wxString &title)
: wxDialog(dlg, -1, title)
{
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
wxBoxSizer* bSizer1;
bSizer1 = new wxBoxSizer(wxHORIZONTAL);
m_staticText1 = new wxStaticText(this, wxID_ANY, wxT("Welcome To\nwxWidgets"), wxDefaultPosition, wxDefaultSize, 0);
m_staticText1->SetFont(wxFont(20, 74, 90, 90, false, wxT("Arial")));
bSizer1->Add(m_staticText1, 0, wxALL|wxEXPAND, 5);
wxBoxSizer* bSizer2;
bSizer2 = new wxBoxSizer(wxVERTICAL);
wxPoint textCtrl1Position(5,5); //Position
wxSize textCtrl1size(120,25); //Size
textCtrl1 = new wxTextCtrl(this, wxID_ANY, "hi", wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, "textCtrl1"); //Create textCtrl
bSizer2->Add(textCtrl1, 0, wxALL|wxEXPAND, 5); //Add to sizer
m_staticline1 = new wxStaticLine(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL);
bSizer2->Add(m_staticline1, 0, wxALL|wxEXPAND, 5);
BtnGo = new wxButton(this, idBtnGo, wxT("&Go"), wxDefaultPosition, wxDefaultSize, 0);
bSizer2->Add(BtnGo, 0, wxALL, 5);
bSizer1->Add(bSizer2, 1, wxEXPAND, 5);
this->SetSizer(bSizer1);
this->Layout();
bSizer1->Fit(this);
}
wxQuestionDialog::~wxQuestionDialog()
{
}
void wxQuestionDialog::OnClose(wxCloseEvent &event)
{
Destroy();
}
void wxQuestionDialog::OnGo(wxCommandEvent &event)
{
somefunction();
}
otherFile.cpp:
#include "wxQuestionMain.h"
void somefunction()
{
//Try to change the text in textCtrl1
wxQuestionDialog::textCtrl1->AppendText("Red text\n");
}
產地:
error: ‘wxTextCtrl* wxQuestionDialog::textCtrl1’ is protected
error: invalid use of non-static data member ‘wxQuestionDialog::textCtrl1’
所以我提出 'wxTextCtrl * textCtrl1;'從 '保護' wxQuestionMain.h'到 '公共'
產地:
error: invalid use of non-static data member ‘wxQuestionDialog::textCtrl1’
在wxQuestionMain.h類似乎最高審計機關 '類wxQuestionDialog:公共wxDialog'
我不不知道這個「公共」部分是什麼意思,我從來沒有見過類創建過,但我會嘗試更改'otherFile.cpp',所以它是wxDialog而不是wxQuestionDialog。
#include "wxQuestionMain.h"
void somefunction()
{
//Try to change the text in textCtrl1
wxDialog::textCtrl1->AppendText("Red text\n");
}
產地:
error: ‘textCtrl1’ is not a member of ‘wxDialog’
我不知所措我在這裏..我怎麼能不添加 「somefunction()」 到wxWidget類更新 「textCtrl1」 的文本?
CodeBlocks自動生成2個其他文件,不知道它們是否重要,但它們在這裏。
wxQuestionApp.cpp
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "wxQuestionApp.h"
#include "wxQuestionMain.h"
IMPLEMENT_APP(wxQuestionApp);
bool wxQuestionApp::OnInit()
{
wxQuestionDialog* dlg = new wxQuestionDialog(0L, _("wxWidgets Application Template"));
dlg->Show();
return true;
}
wxQuestionApp.h
#ifndef WXQUESTIONAPP_H
#define WXQUESTIONAPP_H
#include <wx/app.h>
class wxQuestionApp : public wxApp
{
public:
virtual bool OnInit();
};
#endif // WXQUESTIONAPP_H
我試過這樣做,即使代碼編譯並且不會拋出錯誤,根本沒有任何東西被添加到listbox2。更新我的答案以顯示我所做的更改。 – natli
完全修改我的問題,所以我以前的評論沒有意義了。 – natli