Search code examples
c++wxwidgets

how to set a value to list or text box using wxWidgets library


I've been struggling with converting the numbers on my buttons to the text box for my assignment. Basically what I'm trying to do is when a user presses for example when I click on 7 it should appear in the text box but currently in my Button Pressed method it only puts in a . instead of 7 and I'm not too familiar with the wxWidgets library classes. is there another command or method I can use?

wxBEGIN_EVENT_TABLE(calMain, wxFrame)
EVT_BUTTON(100, ButtonPressed)
wxEND_EVENT_TABLE()
    
calMain::calMain() : wxFrame(nullptr, wxID_ANY, "Calculator UI!", wxPoint(50, 50), wxSize(335, 545))
{
    wxFont font(15, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false);
    m_txt1 = new wxTextCtrl(this,wxID_ANY, " ",wxPoint(0, 21), wxSize(322, 50));
    CalButton = new wxButton(this, 100, "7", wxPoint(30, 156), wxSize(50, 70));
    CalButton->SetFont(font);
}
void calMain::ButtonPressed(wxCommandEvent& evt) 
{
        m_txt1->SetLabelText(CalButton->GetLabelText());
        evt.Skip();
}

Solution

  • You need to use wxTextCtrl::SetValue() (or ChangeValue(), which is very similar, but doesn't generate any events about the change in the text control) to change the value (not the label) of wxTextCtrl.