Search code examples
c++cclasswxwidgetscodeblocks

WxWidgets - Changing texbox from a file other than the main one


NOTE: I completely revised the question and turned it into an example project specifically for this question, so Nicks answer doesn't really make sense anymore. wxQuestionMain.h and wxQuestionMain.cpp are mildly modified wxWidget files, auto generated by Code::Blocks.

go

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 sais '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"

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

Solution

  • addtolistbox2 is a private member method of gfxDialog - even if you did have a properly constructed object, you wouldn't be able to call that method from outside a gfxDialog instance. At the very least you need to move addtolistbox2 to public: from private:, and then call it on a properly constructed instance (the constructor requires arguments but your code doesn't provide them):

    gfxDialog testtime(0, "test");
    testtime.addtolistbox2("somestring");
    

    (The only valid constructor requires a parent dialog and a title string:

    class gfxDialog: public wxDialog
    {
        public:
        gfxDialog(wxDialog *dlg, const wxString& title);
    

    if I remember my wxWidgets properly, the parent may be NULL, but of course you still have to provide the argument)