Microsoft KB Archive/825007

= BUG: You cannot add a user control to a Windows Form in Visual Studio .NET 2003 =

Article ID: 825007

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition

-





SUMMARY
''You may experience problems when you try to add a user control to a Windows Form, or when you open a Windows Form that has a user control. This article describes the behavior that you may experience. It also describes the cause of this behavior and how you can work around it.

This article includes the steps to reproduce the behavior and links to references that contain more information about the SerializationException class, assemblies, how to configure Folder Redirection and roaming user profiles, and how to use the .NET Framework Configuration tool to configure code groups.''



SYMPTOMS
In Microsoft Visual Studio .NET 2003, you may be not be able to add a user control from the Toolbox to a Microsoft Windows Form. Additionally, when you open a Windows Form that contains a user control, or when you try to build a project that has a Windows Form that contains a user control, the user control may disappear. When this problem occurs, you may receive the following error messages in the Task List window:

The variable  is either undeclared or never assigned.

Could not find Type. . Please make sure the assembly that contains this type is referenced. If this is part of your development project make sure that it is properly built.

A System.Runtime.Serialization.SerializationException exception error may also occur. The exception error message contains the following text:

Insufficient state to deserialize the object. More information is needed.

Note These error message examples use the following placeholders:
 * is a placeholder for the name of the user control variable.
 * is a placeholder for the name of the namespace that contains the user control class.
 * is a placeholder for the name of the user control class.

Also, you may notice the error messages after you upgrade a Microsoft Visual Basic 6.0 project to Microsoft Visual Basic .NET 2003. And, this problem does not occur in Microsoft Visual Studio .NET 2002.



CAUSE
Visual Studio .NET 2003 creates temporary files for project-level assemblies such as the assembly that contains the user control. These temporary files are located in the %APPDATA%\Microsoft\VisualStudio\7.1\ProjectAssemblies folder. Visual Studio .NET 2003 then loads the assemblies from the temporary files into memory. If you use folder redirection to redirect your Application Data folder to a folder that is not fully trusted, such as a network shared folder, Visual Studio .NET 2003 cannot load the assembly.

Typically, the problem that is mentioned in the &quot;Symptoms&quot; section occurs if you are using a roaming user profile.



WORKAROUND
Fully trust all files that are located in the ProjectAssemblies folder. To do this, follow these steps on your local computer:  Start the Microsoft .NET Framework 1.1 Configuration tool. The .NET Configuration 1.1 window opens. In the left pane of the .NET Configuration 1.1 window, expand Runtime Security Policy, and then expand Machine. Expand Code Groups, and then expand All_Code. Right-click LocalIntranet_Zone, and then click New. The Create Code Group dialog box appears. Click to select the Create a new code group option. In the Name box, type MyCodeGroup, and then click Next.</li> In the Choose the condition type for this code group box, click URL.</li> To fully trust all files that are located in the ProjectAssemblies folder, type the following text in the URL box:

file://// \Microsoft\VisualStudio\7.1\ProjectAssemblies\*

Note  is a placeholder for the path of the folder that you redirected the Application Data folder to.</li> Click Next.</li> In the Use existing permission set box, click FullTrust, and then click Next.</li> Click Finish.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.

<div class="moreinformation_section">

MORE INFORMATION
Visual Studio .NET 2002 directly loads project-level assemblies into memory without using a temporary file. Therefore, the behavior that is mentioned in the &quot;Symptoms&quot; section does not occur in Visual Studio .NET 2002.

Steps to reproduce the problem
Note Follow these steps after you redirect your Application Data folder to a folder that is not fully trusted.
 * 1) Start Visual Studio .NET 2003.
 * 2) Use Visual Basic .NET to create a Windows Control Library project. By default, a project that is named WindowsControlLibrary1 and a file that is named UserControl1.vb are created.
 * 3) Build the Windows Control Library project to create the WindowsControlLibrary1.dll assembly.
 * 4) On the Tools menu, click Add/Remove Toolbox Items. The Customize Toolbox dialog box appears.
 * 5) Click the .NET Framework Components tab.
 * 6) On the .NET Framework Components tab, click Browse. The Open dialog box appears.
 * 7) Locate and then click the WindowsControlLibrary1.dll assembly that you created in step 3.
 * 8) Click Open, and then click OK. The UserControl1 user control is added to the Toolbox.
 * 9) Use Visual Basic .NET to add a Windows Application project to your solution. By default, a Windows Form that is named Form1 is created.
 * 10) Try to add a UserControl1 user control to the Form1 Windows Form. You cannot add the user control.
 * 11) Open a project that has a Windows Form that contains a user control, such as a Microsoft Visual Basic 6.0 project that you upgraded to Visual Basic .NET 2003.
 * 12) Open the design view of the Windows Form. The user control may disappear. Also, in the Task List window, you may notice the first two error messages that are mentioned in the &quot;Symptoms&quot; section.

<div class="references_section">