tags:

views:

72

answers:

2

I'm passing a reference of a form to a class. Within this class I believed I could use formRef->Controls["controlName"] to access properties on the control.

This works for a few labels, but on a button I receive a "Object reference not set to an instance of an object." when I try to change the Text property.

Help or explanation appreciated...

A: 

That suggests that the control with the given name wasn't found.

Don't forget that the name of the control isn't necessarily the same as its ID in the designer. Check the actual name against the one you're using to look it up with.

Jon Skeet
A: 

I did this, and it's working. Could possibly be safer as I can check if the control actually exists...

array<Control^>^ id = myForm->Controls->Find("myButton", true);
id[0]->Text = "new text";

I think the reason it breaks is that the button is on another panel. I didn't think of that when I posted. The new solution will search all children too, so it's an improvement.

rozon