Checkbox help

22 views
Skip to first unread message

Andre F

unread,
Nov 9, 2022, 2:42:24 AM11/9/22
to wx-u...@googlegroups.com
I've created a program with several checkboxes and they work fine.  My problem is when I try to get or set the state of the checkbox manually.  Can you point me to some sample code using these functions( Get or SetValue())? 

It crashes every time I try to use GetValue or SetValue.  Otherwise the checkboxes work fine.:
checkbox12->GetValue()
checkbox12->SetValue(false)


header file:
private:
void OnCHECKBOX12Event(wxCommandEvent& evt);
wxCheckBox* checkbox12;

main CPP file
#include <wx/checkbox.h>

wxCheckBox* checkbox12 = new wxCheckBox(panel1, wxID_CHECKBOX12, "", wxPoint(360, 388), wxDefaultSize, 0);

void WxButton1Frame::OnButton19Clicked(wxCommandEvent& evt) {

//A bunch of other code

      if (checkbox12->GetValue() == true) {
           checkbox12->SetValue(false);
      }
}

void WxButton1Frame::OnCHECKBOX12Event(wxCommandEvent& evt)
{
     if (evt.IsChecked()) {
     } else {
     }
}

wxBEGIN_EVENT_TABLE(WxButton1Frame, wxFrame)
      EVT_CHECKBOX(wxID_CHECKBOX12, WxButton1Frame::OnCHECKBOX12Event)
wxEND_EVENT_TABLE()

Gunter Königsmann

unread,
Nov 9, 2022, 3:21:44 AM11/9/22
to wx-u...@googlegroups.com, Andre F
I use SetValue() and GetValue() all the time. Are you sure that your pointers to the check boxes are still valid when you call those methods? Not that something overwrites the values of variables your program keeps sometimes.

PB

unread,
Nov 9, 2022, 4:19:05 AM11/9/22
to wx-users
It seems you have a common bug in your code, where you shadow the class member variable with the local declaration.

IOW, replace the code
wxCheckBox* checkbox12 = new wxCheckBox(panel1, wxID_CHECKBOX12, "", wxPoint(360, 388), wxDefaultSize, 0);
with (checkbox12 is already declared as a class member variable)

Andre F

unread,
Nov 9, 2022, 8:30:34 AM11/9/22
to wx-u...@googlegroups.com

Thank you, fixed and working now!  Please close the case.

Andre

--
Please read https://www.wxwidgets.org/support/mlhowto.htm before posting.
---
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/bd1d43e1-1a25-4b5f-8c92-b1dcd6bcaaabn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages