Problem Assigning members to XRC objects

27 views
Skip to first unread message

Tim Burgess

unread,
Sep 10, 2025, 10:26:53 AM (12 days ago) Sep 10
to wx-u...@googlegroups.com

Hi,

 

I’m successfully loading an XRC file that contains subclass entries. The relevant classes contain member variables for elements within the XRC file, such as:

 

class SampleNotebookPage: public wxPanel

{

       wxDECLARE_DYNAMIC_CLASS(SampleNotebookPage);

 

public:

       SampleNotebookPage ();

 

       // Creation

       bool Create()

{

       tcAttributeValue = XRCCTRL(*this, "AttributeValue", wxTextCtrl);

tcAttributeValue->Connect(wxEVT_TEXT, wxTextEventHandler(EBrailleRequiredMetaPage::OnTextChange));

};

       ~ SampleNotebookPage ();

wxTextCtrl * tcAttributeVallue;

}

 

I successfully initialize such members when I create an instance of the class. However, though the handlers fire correctly, trying to reference one of my member variables within the handler leads to exceptions. Here’s an example:

 

void EBrailleRequiredMetaPage::OnTextChange(wxCommandEvent& event)

{

              int id = event.GetId();

 

              if (id == XRCID("tcAttributeValue"))

       {

                      tcAttributeValue= XRCCTRL(*this, "tcAttributeValue", wxTextCtrl);

                     

// Assertion here:

       if (tcAttributeValue->GetValue().Trim().IsEmpty())

       {

              …

       }

       else

       {

       }

       }

}

 

If I try to access the member directly:

 

tcTitle->GetValue();

 

I get an exception.

 

What am I missing here, as I thought I was following all of the examples I’ve found.

 

Many thanks in advance.

 

Tim

 

 

 

Maarten Bent

unread,
Sep 10, 2025, 1:58:03 PM (12 days ago) Sep 10
to wx-u...@googlegroups.com
Hi Tim,

Is your example code trying to reference the same object?
Because in the Create function you use the XRC name "AttributeValue", but in the event handler you use XRC name "tcAttributeValue".

Secondly, you use "tcAttributeValue->Connect". I think this is incorrect. You want to Connect to the event handler of your class, not the event handler of tcAttributeValue.
It should be something like:
Connect(tcAttributeValue->GetId(), wxEVT_TEXT, wxTextEventHandler(EBrailleRequiredMetaPage::OnTextChange));

Even better would be to not use the legacy Connect, but instead use Bind. Which likely will warn about this problem at compile-time instead of run-time.
Bind(wxEVT_TEXT, &EBrailleRequiredMetaPage::OnTextChange, this, tcAttributeValue->GetId());

Hope this helps,
Maarten
--
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 visit https://groups.google.com/d/msgid/wx-users/008f01dc225e%24eef3fd70%24ccdbf850%24%40raisedbar.net.

Tim Burgess

unread,
Sep 11, 2025, 5:40:02 AM (11 days ago) Sep 11
to wx-users
Hi Martin,

Apologies for my sample code - I knocked it together in a rush and was trying to honour my NDA responsibilities.

However, your example using Bind fixed me up a treat, so many thanks.

Tim


Reply all
Reply to author
Forward
0 new messages