Author Topic: WxWidgets C++ - Print out text in a static textbox  (Read 3692 times)

Offline MegAmaNeo

  • Single posting newcomer
  • *
  • Posts: 3
WxWidgets C++ - Print out text in a static textbox
« on: September 19, 2020, 08:41:55 pm »
Hello,

I am writing a WxWidget C++ program where I have a button and a static textbox.
My intention is that after clicking the button, a text is printed out in the static box.
Although I am new to C++ and not (yet!) familiar with the concept of pointers, I read that a quick implementation might be to declare the following to lines inside the event function of the button:

wxStaticText *m_statictext;
m_statictext->SetLabel( "test" );

Unfortunately this doesn't work. Do you know if there is a quick implementation?
I pasted you the code below and attached a photo of the properties of the static textbox:





/***************************************************************
 * Name:      DeutschprojektMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    blank
 * Created:   2020-09-19
 * Copyright: blank ()
 * License:
 **************************************************************/

#include "DeutschprojektMain.h"
#include <wx/msgdlg.h>
#include "Fenster.h"
#include <wx/txtstrm.h>
using namespace std;
//(*InternalHeaders(DeutschprojektFrame)
#include <wx/intl.h>
#include <wx/string.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(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(DeutschprojektFrame)
const long DeutschprojektFrame::ID_BUTTON1 = wxNewId();
const long DeutschprojektFrame::ID_STATICTEXT1 = wxNewId();
const long DeutschprojektFrame::idMenuQuit = wxNewId();
const long DeutschprojektFrame::idMenuAbout = wxNewId();
const long DeutschprojektFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(DeutschprojektFrame,wxFrame)
    //(*EventTable(DeutschprojektFrame)
    //*)
END_EVENT_TABLE()

DeutschprojektFrame::DeutschprojektFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(DeutschprojektFrame)
    wxMenu* Menu1;
    wxMenu* Menu2;
    wxMenuBar* MenuBar1;
    wxMenuItem* MenuItem1;
    wxMenuItem* MenuItem2;

    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    SetClientSize(wxSize(440,476));
    Button1 = new wxButton(this, ID_BUTTON1, _("Label"), wxPoint(72,216), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    StaticText1 = new wxStaticText(this, ID_STATICTEXT1, wxEmptyString, wxPoint(264,152), wxSize(88,105), 0, _T("ID_STATICTEXT1"));
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DeutschprojektFrame::OnButton1Click1);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&DeutschprojektFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&DeutschprojektFrame::OnAbout);
    //*)
}

DeutschprojektFrame::~DeutschprojektFrame()
{
    //(*Destroy(DeutschprojektFrame)
    //*)
}

void DeutschprojektFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void DeutschprojektFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void DeutschprojektFrame::OnButton1Click(wxCommandEvent& event)
{







}

void DeutschprojektFrame::OnButton1Click1(wxCommandEvent& event)
{
}
« Last Edit: September 19, 2020, 08:52:11 pm by MegAmaNeo »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: WxWidgets C++ - Print out text in a static textbox
« Reply #1 on: September 19, 2020, 09:10:54 pm »
General programming questions are off-topic here, but this is loosely related to wxSmith. You must write something like this:

Code
void DeutschprojektFrame::OnButton1Click1(wxCommandEvent& event)
{
   StaticText1->SetLabel( "test" );
}