|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
What is the next event to fire after the OnCreateControl?I am designing an inherited control. Besides the OnCreateControl , what
other events can be listened to that the if(!DesignMode) test willl work? The reason I am asking is that a customer has asked to hide the control. However, setting the controls visible property to false means that the OnCreateControl which contains some essential processing never fires. I need somewhere else to relocate this code, presumably in the next event after the OnCreateControl event. Also, is there is a published list of events in the lifecyle of a winforms control? I can't find anything on this! Hi Greg,
Once you make the control visible by setting Visible = true or calling Show(), OnCreateControl will be called, but only the first time it becomes visible. The "essential processing" probably shouldn't run unless the control is going to be used (i.e., unless Visible = true), otherwise it probably shouldn't be in the control to begin with. -- Show quoteDave Sexton "Greg" <spam_gw001@yahoo.co.uk> wrote in message news:1161698069.228485.114680@k70g2000cwa.googlegroups.com... >I am designing an inherited control. Besides the OnCreateControl , what > other events can be listened to that the if(!DesignMode) test willl > work? The reason I am asking is that a customer has asked to hide the > control. However, setting the controls visible property to false means > that the OnCreateControl which contains some essential processing never > fires. I need somewhere else to relocate this code, presumably in the > next event after the OnCreateControl event. Also, is there is a > published list of events in the lifecyle of a winforms control? I > can't find anything on this! > Thanks Dave.
I know it is an odd request. Basically the control is one of many that fit into a mini IDE that my company produces. A customer has requested that the control works even when invisible (it sends data across a network), since the operator using the app that the IDE creates never needs to see or interact with the control. Due to the nature of the project, the code that needs to be run does need to be in this control, so I'm left wondering what my options are. It is possible to ask the customer to place a panel control over the control to be hidden. Its too much of a kludge in my opinion though. I'd rather do something else, even if it is making the control visible for a moment, and then re-hiding it (again a kludge!). Thanks again, Greg. Dave Sexton wrote: Show quote > Hi Greg, > > Once you make the control visible by setting Visible = true or calling Show(), > OnCreateControl will be called, but only the first time it becomes visible. > > The "essential processing" probably shouldn't run unless the control is going > to be used (i.e., unless Visible = true), otherwise it probably shouldn't be > in the control to begin with. > > -- > Dave Sexton > > "Greg" <spam_gw001@yahoo.co.uk> wrote in message > news:1161698069.228485.114680@k70g2000cwa.googlegroups.com... > >I am designing an inherited control. Besides the OnCreateControl , what > > other events can be listened to that the if(!DesignMode) test willl > > work? The reason I am asking is that a customer has asked to hide the > > control. However, setting the controls visible property to false means > > that the OnCreateControl which contains some essential processing never > > fires. I need somewhere else to relocate this code, presumably in the > > next event after the OnCreateControl event. Also, is there is a > > published list of events in the lifecyle of a winforms control? I > > can't find anything on this! > > Hi Greg,
That's what Components are for. They don't display an interface :) Just because the Control needs to perform some required processing doesn't mean the actual processing should take place in the control. A better design pattern would be to create a business object that can handle the custom processing and simply invoke it from the control. This will allow you to make a Control and Component that do the same thing. The customer can then choose between the two. I assume that you are worried about checking DesignMode because you are getting errors in the IDE when your control is placed on a Form, however OnCreateControl is called even when Visible=false in the IDE, AFAIK. -- Show quoteDave Sexton "Greg" <spam_gw001@yahoo.co.uk> wrote in message news:1161706004.103620.205050@b28g2000cwb.googlegroups.com... > Thanks Dave. > > I know it is an odd request. Basically the control is one of many that > fit into a mini IDE that my company produces. A customer has requested > that the control works even when invisible (it sends data across a > network), since the operator using the app that the IDE creates never > needs to see or interact with the control. Due to the nature of the > project, the code that needs to be run does need to be in this control, > so I'm left wondering what my options are. It is possible to ask the > customer to place a panel control over the control to be hidden. Its > too much of a kludge in my opinion though. I'd rather do something > else, even if it is making the control visible for a moment, and then > re-hiding it (again a kludge!). > > Thanks again, > > Greg. > > > Dave Sexton wrote: >> Hi Greg, >> >> Once you make the control visible by setting Visible = true or calling >> Show(), >> OnCreateControl will be called, but only the first time it becomes visible. >> >> The "essential processing" probably shouldn't run unless the control is >> going >> to be used (i.e., unless Visible = true), otherwise it probably shouldn't >> be >> in the control to begin with. >> >> -- >> Dave Sexton >> >> "Greg" <spam_gw001@yahoo.co.uk> wrote in message >> news:1161698069.228485.114680@k70g2000cwa.googlegroups.com... >> >I am designing an inherited control. Besides the OnCreateControl , what >> > other events can be listened to that the if(!DesignMode) test willl >> > work? The reason I am asking is that a customer has asked to hide the >> > control. However, setting the controls visible property to false means >> > that the OnCreateControl which contains some essential processing never >> > fires. I need somewhere else to relocate this code, presumably in the >> > next event after the OnCreateControl event. Also, is there is a >> > published list of events in the lifecyle of a winforms control? I >> > can't find anything on this! >> > > Thanks again Dave.
Ok....The architecture is not in my hands, though I can see why it has been designed the way it has. I could explain it, but it would take a few days! You are correct about why I check DesignMode - I don't want someone designing an app with the IDE to see error messages - this wouldn't look very professional. However, in when I test the code within Visual Studio, OnCreateControl doesn't fire when the control != visible. This agrees with the symptoms that can be seen when running an app that has been built by the "mini-IDE". Also, the control does need to be a control and not a component. Regards, Greg. Dave Sexton wrote: Show quote > Hi Greg, > > That's what Components are for. They don't display an interface :) > > Just because the Control needs to perform some required processing doesn't > mean the actual processing should take place in the control. A better design > pattern would be to create a business object that can handle the custom > processing and simply invoke it from the control. This will allow you to make > a Control and Component that do the same thing. The customer can then choose > between the two. > > I assume that you are worried about checking DesignMode because you are > getting errors in the IDE when your control is placed on a Form, however > OnCreateControl is called even when Visible=false in the IDE, AFAIK. > > -- > Dave Sexton > > "Greg" <spam_gw001@yahoo.co.uk> wrote in message > news:1161706004.103620.205050@b28g2000cwb.googlegroups.com... > > Thanks Dave. > > > > I know it is an odd request. Basically the control is one of many that > > fit into a mini IDE that my company produces. A customer has requested > > that the control works even when invisible (it sends data across a > > network), since the operator using the app that the IDE creates never > > needs to see or interact with the control. Due to the nature of the > > project, the code that needs to be run does need to be in this control, > > so I'm left wondering what my options are. It is possible to ask the > > customer to place a panel control over the control to be hidden. Its > > too much of a kludge in my opinion though. I'd rather do something > > else, even if it is making the control visible for a moment, and then > > re-hiding it (again a kludge!). > > > > Thanks again, > > > > Greg. > > > > > > Dave Sexton wrote: > >> Hi Greg, > >> > >> Once you make the control visible by setting Visible = true or calling > >> Show(), > >> OnCreateControl will be called, but only the first time it becomes visible. > >> > >> The "essential processing" probably shouldn't run unless the control is > >> going > >> to be used (i.e., unless Visible = true), otherwise it probably shouldn't > >> be > >> in the control to begin with. > >> > >> -- > >> Dave Sexton > >> > >> "Greg" <spam_gw001@yahoo.co.uk> wrote in message > >> news:1161698069.228485.114680@k70g2000cwa.googlegroups.com... > >> >I am designing an inherited control. Besides the OnCreateControl , what > >> > other events can be listened to that the if(!DesignMode) test willl > >> > work? The reason I am asking is that a customer has asked to hide the > >> > control. However, setting the controls visible property to false means > >> > that the OnCreateControl which contains some essential processing never > >> > fires. I need somewhere else to relocate this code, presumably in the > >> > next event after the OnCreateControl event. Also, is there is a > >> > published list of events in the lifecyle of a winforms control? I > >> > can't find anything on this! > >> > > > Hi Greg,
> Ok....The architecture is not in my hands, though I can see why it has Yea, no need :)> been designed the way it has. I could explain it, but it would take a > few days! > You are correct about why I check DesignMode - I don't want someone In VS 2005 OnCreateControl is called when the containing Form is first opened > designing an app with the IDE to see error messages - this wouldn't > look very professional. However, in when I test the code within Visual > Studio, OnCreateControl doesn't fire when the control != visible. This > agrees with the symptoms that can be seen when running an app that has > been built by the "mini-IDE". in the designer, even when Visible = false. This is because VS displays your control even when it's not visible. I just tested it myself: public class TestControl : Control { public TestControl() { BackColor = Color.Green; Visible = false; } protected override void OnCreateControl() { MessageBox.Show("Created!"); base.OnCreateControl(); } } Add TestControl to any Form and see what I mean. > Also, the control does need to be a control and not a component. Here are a few options:1. Place the code that needs to be executed in the constructor instead of OnCreateControl. 2. Derive from UserControl and place code in the OnLoad method instead of OnCreateControl. 3. Set BackColor to Color.Transparent and hide all child controls. -- Dave Sexton Thanks Dave.
Wow, what a nightmare! Ok, I've looked into why it worked for you and not me. When you pressed build and run, in the build stage, the control was been built, and then added to the form. This is where your OnCreateControl was being called. If you put a If(!DesignMode) test around the MessageBox call, the message box never gets shown. Also, for the same reason, if you run the app again without the build, the MessageBox never gets shown (even without the DesignMode test). So, OnCreateControl won't get called if the control is not visible. However, I can see that the protected override void InitLayout() gets called right at the end of the InitializeComponent() of the main form, at which point controls can be interrogated as to whether there are in DesignMode or not. InitLayout will therefore do what I need! Thanks for your help - I've learned quite a bit on this one! Regards, Greg. Dave Sexton wrote: Show quote > Hi Greg, > > > Ok....The architecture is not in my hands, though I can see why it has > > been designed the way it has. I could explain it, but it would take a > > few days! > > Yea, no need :) > > > You are correct about why I check DesignMode - I don't want someone > > designing an app with the IDE to see error messages - this wouldn't > > look very professional. However, in when I test the code within Visual > > Studio, OnCreateControl doesn't fire when the control != visible. This > > agrees with the symptoms that can be seen when running an app that has > > been built by the "mini-IDE". > > In VS 2005 OnCreateControl is called when the containing Form is first opened > in the designer, even when Visible = false. This is because VS displays your > control even when it's not visible. I just tested it myself: > > public class TestControl : Control > { > public TestControl() > { > BackColor = Color.Green; > Visible = false; > } > > protected override void OnCreateControl() > { > MessageBox.Show("Created!"); > base.OnCreateControl(); > } > } > > Add TestControl to any Form and see what I mean. > > > Also, the control does need to be a control and not a component. > > Here are a few options: > > 1. Place the code that needs to be executed in the constructor instead of > OnCreateControl. > 2. Derive from UserControl and place code in the OnLoad method instead of > OnCreateControl. > 3. Set BackColor to Color.Transparent and hide all child controls. > > -- > Dave Sexton Hi Greg,
> Ok, I've looked into why it worked for you and not me. When you pressed OnCreateControl will get called even if the control is not visible, as I've > build and run, in the build stage, the control was been built, and then > added to the form. This is where your OnCreateControl was being called. > If you put a If(!DesignMode) test around the MessageBox call, the > message box never gets shown. Also, for the same reason, if you run the > app again without the build, the MessageBox never gets shown (even > without the DesignMode test). > > So, OnCreateControl won't get called if the control is not visible. shown. It won't get called if the control is not built, but it must be built to be added to the Form in the first place, so I'm not sure what you mean. > However, I can see that the protected override void InitLayout() gets Yes, it seems InitLayout is the perfect place for your code. From > called right at the end of the InitializeComponent() of the main form, > at which point controls can be interrogated as to whether there are in > DesignMode or not. InitLayout will therefore do what I need! Control.InitLayout on MSDN: The InitLayout method is called immediately after adding a control to a container Good find! > Thanks for your help - I've learned quite a bit on this one! Glad to help :)-- Dave Sexton |
|||||||||||||||||||||||