rozon
rozon

Reputation: 2538

Modifying controls with Form.Controls

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...

Upvotes: 0

Views: 207

Answers (3)

Jason
Jason

Reputation: 6926

You could always try static forms ;D

Upvotes: 0

rozon
rozon

Reputation: 2538

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503954

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.

Upvotes: 1

Related Questions