I was just wondering if I’m doing this the correct way. I have 2 forms a parent form and a child form (options dialog). To change a property in my parent form from my child form I use code like this:
// Create an array of all rich textboxes on the parent form.
var controls = this.Owner.Controls.OfType<RichTextBox>();
foreach (var item in controls) {
if (chkDetectUrls.Checked)
((RichTextBox)item).DetectUrls = true;
else
((RichTextBox)item).DetectUrls = false;
}
I only have one RichTextBox on my form. It seems silly to have to loop through a array of 1 control. Is this the correct way to do it or is there an easier way?
It’s not appropriate to change properties in a parent form at all. Instead, your child form should raise an event which the parent form listens for, and changes its own value accordingly.
Manipulating the parent form from the child creates a two-way coupling – the parent form owns the child, but the child also has intimate knowledge and dependency on the parent form. Bubbling is the established solution to this, as it allows information to flow upwards (‘bubbling’) while avoiding any strict coupling.
Here is the most basic example of eventing. It does not include passing specific information in the event (which is what you may need) but covers the concept.
In your child form:
And in your parent form:
As I said above, you probably need to pass some specific information in your event. There’s a few ways we can do this, but the simplest one is to create your own EventArgs class and your own delegate. It looks like you need to specify whether some value is set to true or false, so let’s use that:
We can change our event to use these new signatures:
And we pass our custom EventArgs object:
And we change our event handling in the parent accordingly: