Winform Custom Controls - Designer randomly stops initializing / calling constructor
c#, custom-controls, visual-studio, visual-studio-2010, winforms
Solution
First, the problem... you are putting stuff in the ".designer.cs" code... Don't. That's Microsoft's sandbox. When WinForms forms and visual controls are created, they are typically in a "set" such as
MyForm.cs (place for YOUR code)
MyForm.designer.cs (Microsoft's sandbox area for designer stuff)
MyForm.resx (Resource file -- if used, specific to this form).
That said, if you open your WinForm via the normal "Designer", and right-click "view code", it will bring up the MyForm.cs code set. Notice its a
PARTIAL PUBLIC CLASS MyForm : Window
{
public MyForm()
{
InitializeComponent();
}
}
The "MyForm.Designer.cs" and "MyForm.cs" basically work as a team. Your stuff in addition to Microsoft's stuff. Depending on what you are trying to do, you can hook it in here in THIS constructor for the overall form. If you need to prepare some data connection, query data, populate lists or whatever that will be used in the form. you can do in many ways (either before or after).
public MyForm()
{
DoYourStuff();
InitializeComponent();
// or DoYourStuff here after designer controls are all initialized.
}
private void DoYourStuff()
{ put it here }
Sometimes you may want to do things after the entire form is loaded, but before final presentation to the user. Sometimes, I've actually put a hook after the LOADED is complete for the entire form (the form and ALL it's controls). So I've done something like:
public MyForm()
{
Load += MyAfterInitializeComponents;
InitializeComponent();
}
private void MyAfterInitializeComponents(object sender, EventArgs e)
{
DoYourStuff();
}
If there's something specific you are trying to do, maybe some sample code to help integrate within YOUR sandbox of window-based code.
Problem
This is a freaking nightmare and I'm desperately needing help. I've looked EVERYWHERE for help NOTHING has worked as a permanent fix. I've got some custom controls I've developed; controls that extend button, user control, etc. I have an issue that VS2010 constantly and randomly, with no rhyme or reason that I can discern, decides when I try and open the design view of a control which uses the custom control that it (EDIT: 'it' being Windows Forms generator) should COMPLETELY REMOVE THE LINE which calls the constructor for the custom control from the .designer.cs file, thus completely breaking my ability to view the Design View. The application still builds and runs completely fine. I've tested and ruled out the following as issues: Public constructor - evidently having a non public (internal, etc.) constructor can cause issues like this. I've checked 100 times - ALL the custom controls have public constructors. Empty constructors - in addition to being public, it needs to be empty. No calling anything that could fail in the constructor - I've reduced ALL constructors for custom controls to empty ``` Name() { } ``` calls. No luck there. Namespaces - evidently being in a different namespace can cause problems? I'm in the same one, and the files are IN THE SAME PROJECT. Unload project. Close VS. Delete .suo intellisense file. Delete obj files. Open VS. Reload project. Rebuild project. Still no luck. This is INSANELY frustrating. It's to the point I'm ready to throw out using custom controls and SKIN EVERY CONTROL INDIVIDUALLY BY HAND TO AVOID IT. Does ANYONE have ANY knowledge of what causes this?