VS question about the auto generated InitializeComponent() method
-
Hi. I have a question regarding the auto-generated InitializeComponent() code when using visual studio. For example, if I have a form and a user control. Now, when I drag the user-control onto the form VS automatically instanciates the user-control inside the InitializeComponent() method. But say I have a property in the user-control I want to set through it's constructor, ie. passing some parameter when istansiating the user-control. How can I do this without editing the InitializeComponent() in the code-editor?
-
Hi. I have a question regarding the auto-generated InitializeComponent() code when using visual studio. For example, if I have a form and a user control. Now, when I drag the user-control onto the form VS automatically instanciates the user-control inside the InitializeComponent() method. But say I have a property in the user-control I want to set through it's constructor, ie. passing some parameter when istansiating the user-control. How can I do this without editing the InitializeComponent() in the code-editor?
To use your UserControl within the VS Designer it has to have a default constructor. I see two solutions to your problem: 1) let everything the way it is now and add a property set what you have to set. 2) Add your UserControl dynamically (with the constructor with your parameters) in the Load event of your Form. If you want designer support then I'd suggest you use solution 1. The property value can be set either in the designer (if it is a constant value) or somewhere in the code. Happy coding
-^-^-^-^-^-^-^-^-^-^-^- no code is free of bugs