Re: Problem Assigning members to XRC objects

Tim Burgess <[email protected]> Thu, 11 Sep 2025 10:39:43 +0100
Newsgroups gmane.comp.lib.wxwindows.general
Message-ID <[email protected]>
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


> On 10 Sep 2025, at 18:57, Maarten Bent <[email protected]> wrote:
> 
> 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
> 
> On 10 Sept 2025 16:26, Tim Burgess wrote:
>> 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
>>  
>>  
>>  
>> -- 
>> 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 [email protected] <mailto:[email protected]>.
>> To view this discussion visit https://groups.google.com/d/msgid/wx-users/008f01dc225e%24eef3fd70%24ccdbf850%24%40raisedbar.net <https://groups.google.com/d/msgid/wx-users/008f01dc225e%24eef3fd70%24ccdbf850%24%40raisedbar.net?utm_medium=email&utm_source=footer>.
> 
> 
> -- 
> 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 [email protected] <mailto:[email protected]>.
> To view this discussion visit https://groups.google.com/d/msgid/wx-users/f5d6d539-b8bd-474e-a1c4-8ff4e7e666a5%40gmail.com <https://groups.google.com/d/msgid/wx-users/f5d6d539-b8bd-474e-a1c4-8ff4e7e666a5%40gmail.com?utm_medium=email&utm_source=footer>.

-- 
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 [email protected].
To view this discussion visit https://groups.google.com/d/msgid/wx-users/2E58AB1B-CD69-4B46-8788-CD6CA103BB67%40gmail.com.