SEVI Posted March 30, 2004 Posted March 30, 2004 Hi, I have a SDI project with one form and mulitple panels seperating the form into the various panes. I've written each logical application role as a separate user control, so I can use it in design-mode. I'd like to pass the top level and the immediate Parent form to all Child components, quite simple, I know. But, I've tried doing this mulitple ways and keep running into problems. The latest is using the TopLevelControl property and trying to cast it to the form class I am using. I have placed the code in the FormLoad event as if I placed it in the form constructor (I think because the TopLevelControl does not get populated the form has been initialised. Is this correct?) it fails. With this code I receive a "Specified cast is not valid" message, only in the task list. When I click on the task it goes to line 0 of a class in the project. The project will run until ok until the design time component is altered then it will fail. if (this.TopLevelControl != null) { this.oMainForm = (frmMain)this.TopLevelControl; } if (this.Parent != null) { this.oParentObject = (ParentObject)this.Parent; } I have tried to pass the parameter as a property but it gets messy as it has to transverse through all Child objects. I have also tried to pass the form as a constructor parameter but, the parameter is always overridden by the IDE if a change is made to a property. Can I force a constructor to pass a parameter when the component has a design interface. What am I doing wrong?? Please help!! Thanx .. SEVI Quote
mocella Posted March 30, 2004 Posted March 30, 2004 You'll have better luck if you create your own overload constructor and leave the IDE generated one alone (been there myself). Here's how it'd look: private Form _someFormInstanceHolder; public SomeClass( ) { // // Required for Windows Form Designer support // InitializeComponent(); } public SomeClass( Form someFormInstance ):this() { //store user struct in global variable _someFormInstanceHolder= someFormInstance ; } Quote
SEVI Posted March 30, 2004 Author Posted March 30, 2004 Thanks Mocella, I have that bit ok. The problem is how do I tell the IDE to use the second constructor. I've tried to illustrate one of the solutions that I attempted based on the example in your feedback. Problem is that the (this) parameter is not persistant as it is being managed by the IDE. When you drag an object from the toolbar, by default it builds the object using the default constructor, even if it is changed, it will change back if another property is updated. I'm wanting to know if I can tell the IDE to make another constructor persistant. I can do it building the object with code but I want to be able to use the IDE. public class Form { public Form() { // // Required for Windows Form Designer support // InitializeComponent() { SomeClass sc = new SomeCl***(this); } } } public class SomeClass { public SomeClass( ) { } public SomeClass( Form someFormInstance ):this() { //store user struct in global variable _someFormInstanceHolder= someFormInstance ; } } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.