crankyslap 0 Newbie Poster

When I click the "Go" button I want the button event in "wxQuestionMain.cpp" to call "somefunction()" which is inside "otherFile.cpp". That works just fine. But I then want to change the text in the textbox "txtCtrl1" from inside "somefunction()" and that won't work because "somefunction()" is not part of the wxWidget class, and I don't want it to be. The wxwidget class is created in "wxQuestionMain.h".

wxQuestionMain.h -> Just creates the class

#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 -> Lots of yadda yadda and then at the very bottom the function that handles the buttonclick event.

#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");
	}

Produces:

error: ‘wxTextCtrl* wxQuestionDialog::textCtrl1’ is protected
	error: invalid use of non-static data member ‘wxQuestionDialog::textCtrl1’

So I moved 'wxTextCtrl* textCtrl1;' in 'wxQuestionMain.h' from 'protected' to 'public'

Produces:

error: invalid use of non-static data member ‘wxQuestionDialog::textCtrl1’

The class in wxQuestionMain.h seems to say 'class wxQuestionDialog: public wxDialog'

I don't know what that "public" part means, I've never seen a class be created like that before, but I'm going to try to change 'otherFile.cpp' so it sais wxDialog instead of wxQuestionDialog.

#include "wxQuestionMain.h"
	#define BOOST_FILESYSTEM_VERSION 2

	void somefunction()
	{
	    //Try to change the text in textCtrl1
	    wxDialog::textCtrl1->AppendText("Red text\n");
	}

Produces:

error: ‘textCtrl1’ is not a member of ‘wxDialog’

I'm at a loss here.. how can I update the text in "textCtrl1" without adding "somefunction()" to the wxWidget class?

CodeBlocks auto generated 2 other files, not sure if they are important, but here they are.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.