When is oncreatecontrol called




















I have found that in special cases can Onload be called before OnCreateControl. This case is Contorl. Showing a form: Control. HandleCreated Control. BindingContextChanged Form. Load Control. VisibleChanged Control. GotFocus Form. Activated Form. Shown Closing a form: Form. Closing Form. FormClosing Form. Closed Form. FormClosed Form. Deactivate Control. LostFocus Control. HandleDestroyed Component. Patrik Svensson Patrik Svensson 13k 8 8 gold badges 55 55 silver badges 77 77 bronze badges.

I have seen this behavior, may have something to do with MDI: form. Handle is x form. Handle is still x , then OnHandleCreated this. The whole point of this thread was to set up some controls during the form constructor. I needed the OnCreate functionality of the controls to run during this setup, but couldn't seem to get it to happen without accessing the control handle.

Now, in the test app, I need very little to run during the constructor, other than creating the control, but I still need that OnCreate functionality to occur before doing some subsequent setup. As long as I shift the subsequent setup to the form's Shown event, I don't even need to access the control handle. OnCreate appears to be raised by the control when the form constructor completes, but I can't seem to force it to be raised any earlier than that, even though I was able to in the main app.

It's a weird situation. Then use a button to add an instance to a Form's controls collection. The message appears when you click the button. The message also appears when you drag an instance of the control onto a Form, Panel etc.

The two scenarios you have suggested actually do work the way you noted, as you probably realize. However, the underlying mechanism is not quite as you suggest. That's not true. Adding the control to the Controls collection doesn't do it either. Adding the control to the designer does do it, but I can't do that for this kind of control for reasons that are also bizarre XNA based controls can't display in the designer without adding some awkward intermediate steps due to what appears to be a bug.

In the earlier thread, I had figured out that the OnCreate was fired when the handle was referenced. This happens when you add the control in the designer, because the control is created and painted in the designer window. If the control is created dynamically, the handle isn't referenced when the control is created, but it should be referenced when the control is added to the Controls collection. That wasn't happening, either, so I had to reference the handle directly.

That's where I left it in the original thread. When I tried it most recently, I found that the handle is referenced when the control is added to the controls collection, but doing that in the constructor of the form that holds the control still doesn't cause the OnCreate event to fire. In your case, you are talking about adding a custom control on a button click. I'm not surprised that all will be well at that time.

I would expect that to work fine. I had a LoadContent method that required that the graphics device be initiated, which is what happens in the OnCreate of this control. If LoadContent gets called before OnCreate is called, it crashes, because the graphics device is Nothing though the error is misleading.

What I found was that calling LoadContent in the constructor was causing failure, but moving LoadContent to the Shown event worked fine. In between the end of the constructor and the Shown event, the control raised the OnCreate event.

This was not happening as a result of creating the control, adding the control to the controls collection, or referencing the control handle. By doing all the work after the form constructor was finished, I would say that you bypassed whatever is going on in the constructor.

InitializeComponent ' Add any initialization after the InitializeComponent call. Presumably it can't create a Graphics Device while the. Net Framework is already trying to do so or something like that.

In my main app, I AM causing the OnCreate sub to be raised in a method that is calld by the constructor and therefore runs prior to the Load event. It's only in the test app where I'm having problems with that. Moving the LoadContent call to Shown works, and I would expect that moving it to Load would also work of course, if it didn't, I'd have to deal with the exception. I'm not sure what the difference is. The control itself is the same in both cases, as it is found in a dll referenced by both projects, so it's the same control.

The only difference is the behavior of the two. In the main app, I have to add a line to read the Handle of the control to trigger the Create. That step appears to be vital to the main app, and both irrelevant and insufficient to the test app. Anyways, it's working. The real point to these posts was to update the notes made earlier in the thread to indicate that accessing the control handle was not always sufficient. As you have shown, it also isn't always necessary to perform ANY extra steps.

It may well be due to the XNA control itself, as it already doesn't work the same way as a normal user control in many respects such as the fact that it doesn't show in the designer. I've had no problems initializing a GraphicsDevice in the constructor of a control but in my case, I'm only concerned with runtime behavior. It doesn't draw for some reason at design time and I've never tried to solve it.

Meanwhile C programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". No arguing about the benefits of VB6 over. NET here please. Happiness must reign. Technically, all but one of my XNA controls are dynamic.

Because that one is not, I can't use the form designer. If I made that one dynamic, as well, then I wouldn't have a problem showing the designer I did look into why the designer has problems with drawing XNA controls.

The reason was that they didn't want to grab the graphics device and do initialization setup for design mode. However, I found some other threads that said that there was a bug that prevented directly showing XNA controls in the designer and that you had to add another control in the middle. Few suitable choices for a ToolStripItem derived control. They run when the Visible property of the ToolStripItem changes.

Beware that it may fire multiple times, keep a bool field that tracks that your initialization code has already run. Last but not least, be sure to only do any of this if your code actually requires the control to be created. The vast majority of init code can go in the constructor.

You only need a Load event if your code depends on the actual Location and Size of the control. Which might be different from the designer value if the form rescales itself due to a different system font or video DPI setting on the target machine. I needed a solution like this for a TabPage within a TabControl. The only thing I came up with was using the paint event handler. I added the event handler for Paint and in the very first line I remove the event handler and then do more initialization code.

This only works if you do nothave any custom painting. Alternatively, if you do need to do custom painting you could add a flag to check for each time Paint Executes. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 11 years, 11 months ago. Active 9 years ago. Viewed 6k times.



0コメント

  • 1000 / 1000