UI Updating

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
cszalwinski
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sun Dec 11, 2022 6:00 pm

UI Updating

Post by cszalwinski »

Please point me to some samples that update UI in a dialog. I would like to disable certain textctrls if the user clicks a checkbox.

Thanks,
Chris
User avatar
doublemax
Moderator
Moderator
Posts: 18004
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: UI Updating

Post by doublemax »

What's the part you're having problems with?

To disable a control, you call wxWindow::Disable()
https://docs.wxwidgets.org/trunk/classw ... f01f66c41e

To detect a click on a wxCheckBox, you catch the wxEVT_CHECKBOX event:
https://docs.wxwidgets.org/trunk/classwx_check_box.html
Use the source, Luke!
cszalwinski
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sun Dec 11, 2022 6:00 pm

Re: UI Updating

Post by cszalwinski »

I understand the references you've cited, but I'm having difficulty understanding where to place the code. So I thought an example or pseudo code would clear that up for me.
User avatar
doublemax
Moderator
Moderator
Posts: 18004
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: UI Updating

Post by doublemax »

cszalwinski wrote: Mon Jan 23, 2023 5:12 pm I understand the references you've cited, but I'm having difficulty understanding where to place the code. So I thought an example or pseudo code would clear that up for me.

Code: Select all

#include <wx/wx.h>

class MyApp : public wxApp
{
public:
  virtual bool OnInit();
};

wxIMPLEMENT_APP(MyApp);

class MyFrame : public wxFrame
{
public:
  MyFrame() : wxFrame(NULL, wxID_ANY, "Minimal Sample")
  {
    wxPanel *panel = new wxPanel(this, wxID_ANY);

    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);

    m_checkbox = new wxCheckBox(panel, wxID_ANY, "enabled");
    m_checkbox->SetValue(true);
    m_checkbox->Bind(wxEVT_CHECKBOX, &MyFrame::OnCheckBox, this);
    sizer->Add(m_checkbox, 0, wxEXPAND|wxALL, 4);

    m_text = new wxTextCtrl(panel, wxID_ANY, "some text");
    sizer->Add(m_text, 0, wxEXPAND|wxALL, 4);

    panel->SetSizer(sizer);
  };

  void OnCheckBox( wxCommandEvent &evt )
  {
    m_text->Enable( m_checkbox->GetValue() );
    m_text->Refresh();        // not always necessary, just to be safe
  }

protected:
  wxTextCtrl *m_text;
  wxCheckBox *m_checkbox;
};

bool MyApp::OnInit()
{
   MyFrame* frame = new MyFrame();
   frame->Show(true);

   return true;
}
Use the source, Luke!
cszalwinski
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sun Dec 11, 2022 6:00 pm

Re: UI Updating

Post by cszalwinski »

Thank you! Works perfectly now. The Bind statement was missing in my logic.

Much appreciated,
Chris
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 6551
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: UI Updating

Post by ONEEYEMAN »

Hi,
If you don't call Bind() or don't use static event table, you program doesn't know anything about how to react to the event needed.
Bind() (and static event table macros) instructs you program what to do in case the particular event happens.

Thank you.
Post Reply