pointer to another control value Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
enricovittorini
Knows some wx things
Knows some wx things
Posts: 34
Joined: Mon Nov 07, 2022 2:54 pm

pointer to another control value

Post by enricovittorini »

Hi all,
I would have a question. I need to create a new control X whose "value" will should be a pointer to another wxcontrol Y
wxTextCtrl Y = new wxTextCtrl(m_leftPanel, wxID_ANY, "0.00", wxDefaultPosition, wxDefaultSize, wxTE_RIGHT | wxTE_READONLY)
wxTextCtrl X = new wxTextCtrl(m_leftPanel, wxID_ANY, --here the pointer to Y value--- , wxDefaultPosition, wxDefaultSize, wxTE_RIGHT | wxTE_READONLY)
the idea is that when Y->SetValue("1234") is set, then X control value is updated automatically.

Is it possible?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: pointer to another control value

Post by PB »

No, you have to handle this by yourself by Bind()ing wxEVT_TEXT from the Y control and copying the value from the Y to X control in the handler.

For example, something like this

Code: Select all

#include <wx/wx.h>

class MyDialog : public wxDialog
{
public:
    MyDialog(wxWindow* parent = nullptr) : wxDialog(parent, wxID_ANY, "Test")
    {
        wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);

        m_input = new wxTextCtrl(this, wxID_ANY, "Test");
        mainSizer->Add(m_input, wxSizerFlags().Border());

        m_mirror = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
        mainSizer->Add(m_mirror, wxSizerFlags().Border());
        m_mirror->ChangeValue(m_input->GetValue());
        m_input->Bind(wxEVT_TEXT, [this](wxCommandEvent&)
            {
                m_mirror->ChangeValue(m_input->GetValue());
            });
        
        mainSizer->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), wxSizerFlags().Border());
        
        SetSizerAndFit(mainSizer);
    }
private:
    wxTextCtrl* m_input;
    wxTextCtrl* m_mirror;
};

class MyApp : public wxApp
{
public:	
    bool OnInit() override
    {
        MyDialog().ShowModal();        
        return false;
    }
}; wxIMPLEMENT_APP(MyApp);
enricovittorini
Knows some wx things
Knows some wx things
Posts: 34
Joined: Mon Nov 07, 2022 2:54 pm

Re: pointer to another control value

Post by enricovittorini »

Thanks for the feedback!
Post Reply