Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, March 4, 2012 10:20 AM
I added UserControl1.cs (default User Control) to Class Library. I would like to use this user control in second project (Winforms app). When I'm adding user control to form (using drag&drop) designer is showing following error:
Failed to create component 'UserControl1'. The error message follows: 'System.NullReferenceException: Object reference not set to an instance of an object. at System.ComponentModel.Design.DesignerHost.CreateSite(IComponent component, String name) at System.ComponentModel.Container.Add(IComponent component, String name) ...
Screenshot of full error: http://dl.dropbox.com/u/34289839/error.PNG
Feel free to correct my english. I'll be grateful! [email protected]
All replies (9)
Sunday, March 4, 2012 1:40 PM âś…Answered
Hi Michal,
I can add those controls into a form.. amazing problem :D :D
Add simple a new Windows Form Aplication to your solution.. and add the reference of the Wizard class library project to the newly project.
In that way it works to me :D
I guess the problem is something with the Krok folder in your existing project (WizardAplication).. I tried it already to delete then execute a 'clean' command to the project and build.. then I was able to add your controls to the form
for further problems don't hesitate
with regards Muli :)
If some code doesn't work, don't worry help is on the way.. don't forget to mark your thread as solved when done...
Sunday, March 4, 2012 11:33 AM | 3 votes
When a User Control won't load into the Visual Studio designer here is what you need to do. These instruction are for vb.net project but c# should be similar. Also, before doing this close all open windows (or at least the source and designer files of the control you are working on.)
One last thing. The FIRST thing you should do is ensure that restarting visual studio doesn't fix the problem. If not you can try the steps that follow. These instructions assume that the errant user controls are in control library project in visual studio. If not you should be able to adjust the directions a bit to get it to work but it is much easier when the control is in its own project.
Do the following:
- Make the control library your startup project.
- Open the properties for the control library project and click on the debug tab.
- Under Start Action click the Start external program option and browse to the Visual Studio executable.
NOTE: what this means is that when you run your solution it will fire up another instance of Visual Studio instead of actually running your solution. The First Instance of Visual Studion (INSTANCE_1) will "host" a second instance of visual studio (INSTANCE_2) when you run it.
- Run your solution. INSTANCE_2 will load.
- Switch back to INSTANCE_1.
- In INSTANCE_1 hit CTRL-ALT-E. This will open up the exceptions dialog box. Check On the THROWN column checkbox next to Common Language Runtime Exceptions.
NOTE: This will ensure that INSTANCE_1 will BREAK at ANY runtime error even if it is hit in a try block.
- Switch to INSTANCE_2. In Solution Explorer double-click to open the errant user control.
You should find that INSTANCE_1 OF Visual Studio should have stopped at the line of code that caused the designer to not load the control. Fix the code (which usually means testing for IsNot Nothing before references an object properties...but could mean other things.)
Also, sometimes I find that the control WILL load in INSTANCE_2 instead of breaking on an error in INSTANCE_1. In that case just stop debugging...close INSTANCE_2. Save/Restart INSTANCE_1 and your problem will often have gone away.
The lesson is this. User Control MUST be able to load/reference all objects and their members in order to load it into the designer. So for User Controls that will be placed onto other containers I will usually design events to notify the parent rather than trying to push objects into the child control.
Hope this helps for future reference on this old question.
Mitja
Sunday, March 4, 2012 11:58 AM | 1 vote
Hello,
You should handle the designer by checking whether you are in Design Mode; enclose the logic that gets executed by 'LicenseManager.UsageMode != LicenseUsageMode.Designtime' or using the DesignMode property.
Eyal (http://shilony.net), Regards.
Sunday, March 4, 2012 12:05 PM | 1 vote
Hi,
Just remove property calls from your constructor of the control, it could be null.. that should work :D
If some code doesn't work, don't worry help is on the way.. don't forget to mark your thread as solved when done...
Sunday, March 4, 2012 12:23 PM
Hi, thanks everyone.
Muli_352: my User control doesn't have explicit constructor:
namespace Wizard
{
partial class UserControl1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
}
#endregion
}
}
Mitja Bonca: I added to Form1.Designer.cs one field:
private UserControl1 userControl1;
and this.Controls.Add(userControl1);
in InitializeComponent()
After setting VS as you said VS is throwing TypeLoadException Could not load type 'Wizard.UserControl1' from assembly 'Wizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Whole solution is here: http://dl.dropbox.com/u/34289839/Wizard.rar
Eyal-Shilony: could you share some code?
Feel free to correct my english. I'll be grateful! [email protected]
Sunday, March 4, 2012 2:13 PM | 1 vote
The problem might be the namespaces and your project structure.
Both of your projects ApplicationWizard and Wizard are using the same namespace (Wizard) and in both of them you got a directory and a control that are named Krok that creates a conflict.
Eyal (http://shilony.net), Regards.
Sunday, March 4, 2012 2:15 PM
Muli_352, I added new Winforms application and it works now... But deleting Kroki folder doesn't fix the problem ;-) I checked two winforms apps using diff, but can't see differences - quite strange ;-) But most importantly - your solution works, thank you! :-)
Feel free to correct my english. I'll be grateful! [email protected]
Sunday, March 4, 2012 2:26 PM
Hi Michal,
I'm glad to hear that my solution work :D...
If some code doesn't work, don't worry help is on the way.. don't forget to mark your thread as solved when done...
Thursday, August 1, 2019 8:44 AM
One last thing. The FIRST thing you should do is ensure that restarting visual studio doesn't fix the problem. MAGIC!!