Geting the name of the form a user control is on
-
Hi I've created a number of user controls to use on forms. What i need to do now is get a enum of variables from the form onto the user controls. I am trying to write the code in the user control that just looks for a Enum with a fixed name on the form. Any ideas? The problem i want to be able to reuse these controls on any form aslong as the Enum is there.
Chris
-
Hi I've created a number of user controls to use on forms. What i need to do now is get a enum of variables from the form onto the user controls. I am trying to write the code in the user control that just looks for a Enum with a fixed name on the form. Any ideas? The problem i want to be able to reuse these controls on any form aslong as the Enum is there.
Chris
You can access a user control's parent's properties using the
Parent
property of the UserControl. If you need to access something not part of theForm
class you'll need to cast the property to the Form type, but be warned, this can hinder reusing the control. -
You can access a user control's parent's properties using the
Parent
property of the UserControl. If you need to access something not part of theForm
class you'll need to cast the property to the Form type, but be warned, this can hinder reusing the control.Hi Tony Thanks for the help. The Parents propertiy it not something i have come accross before. Looking at a few points of the web and msdn. I've manged to get the name of the control that the user control is on but not access the properties.
Chris
-
Hi Tony Thanks for the help. The Parents propertiy it not something i have come accross before. Looking at a few points of the web and msdn. I've manged to get the name of the control that the user control is on but not access the properties.
Chris
Hi Chris Have you thought about passing the information from the form to the user control via a property. As Tony mentioned, trying to access in the way you are proposing will cause difficulties in reusability. using a property will make it easier to pass information back to the form (if required). Regards
The FoZ
-
Hi Chris Have you thought about passing the information from the form to the user control via a property. As Tony mentioned, trying to access in the way you are proposing will cause difficulties in reusability. using a property will make it easier to pass information back to the form (if required). Regards
The FoZ
Hi TheFoz Yes I no this would be the easiest way but would require some code to be added the form everytime you used the control. What i was trying to do was have have enum on the main form. When the user control is droped on the form for it o read the enum and add it to a property of the user control. This way i could have multiple copies of the user control on the form and one of the properties of the user control would allow me to select from the forms enum.
Chris
-
Hi TheFoz Yes I no this would be the easiest way but would require some code to be added the form everytime you used the control. What i was trying to do was have have enum on the main form. When the user control is droped on the form for it o read the enum and add it to a property of the user control. This way i could have multiple copies of the user control on the form and one of the properties of the user control would allow me to select from the forms enum.
Chris
I see! Using this code in one of my custom controls I changed the colour of the form to alice blue when the user hovered their mouse on it
((Form)this.Parent).BackColor = Color.AliceBlue;
Using that you should be able to access the enum on the form regards
The FoZ
-
I see! Using this code in one of my custom controls I changed the colour of the form to alice blue when the user hovered their mouse on it
((Form)this.Parent).BackColor = Color.AliceBlue;
Using that you should be able to access the enum on the form regards
The FoZ
Hi Thanks for that. That could be very usefull but it only gives you access to standard windows form properties.
Chris