Control accessing containing form's properties

Raising events on the UC and passing them to the form is one way. Can you be more specific?
 
The user control will need to hold a reference to it's parent, create a property in the UC of type Form and set that to the current form in the form's code for that control. Then in the control simply use that variable.
 
VB.NET:
Me.ParentForm.Text = "test"
 
When I try that I get an error: "ParentForm is not a member of UserControlName"

Edit: Nevermind, it was because I was inheriting from a Panel.
 
When I try that I get an error: "ParentForm is not a member of UserControlName"

Edit: Nevermind, it was because I was inheriting from a Panel.
ParentForm is a property of ContainControl class, which UserControl class inherits. Panel does not inherit this, it's inheriting ScrollableControl/Control, so it only has the Parent property (type Control). You could go from Parent to Parent until you reach a control of type Form, or as JB suggested pass the reference to the form/control you need access to. As newguy suggested it may be better to raise an event from the control that the consumer of your control can use for it's needs, instead of creating a dependency in your control, it's hard to tell without knowing what you're actually trying to do.
John, this is the third or fourth time in the last three weeks you've jumped in with an ulta-easy solution to things here, am I slipping that badly??
Really? I don't think so :)

I also have to mention there is a TopLevelControl property. And a GetContainerControl method...
 
Back
Top