UserControl and interface implementation
-
Hi, I've found a very strange problem and I badly need a fix for it. I have a simple interface defined in one class library, let's say WindowsLibrary1: [code] namespace WindowsLibrary1 { public interface ITest { void Test(); } } [/code] I reference this class library from another project that is a WindowsApplication, lets say: WindowsApplication1. In the WindowsApplication1 I create a simple usercontrol: UserControl1: [code] public class UserControl1 : System.Windows.Forms.UserControl, WindowsLibrary1.ITest { .... public void Test() { // implementation of the WindowsLibrary1.ITest.Test() } } [/code] Ofcourse all compiles fine, but I can not use this usercontol at all. Whenever I try to drop the control on a form I get the following error message: --------------------------- Microsoft Development Environment --------------------------- The user control 'WindowsApplication3.UserControl1' could not be loaded. Ensure that the library containing the control has been built and a project reference has been made to the library containing the control. If you have changed the name of the user control, close and re-open the control's designer to update the toolbox item. --------------------------- OK --------------------------- I badly need this to make it work, so any help is greatly appreciated. Thanks, Corneliu. PS>> Environment: VS.Net 2003, WinXP.
-
Hi, I've found a very strange problem and I badly need a fix for it. I have a simple interface defined in one class library, let's say WindowsLibrary1: [code] namespace WindowsLibrary1 { public interface ITest { void Test(); } } [/code] I reference this class library from another project that is a WindowsApplication, lets say: WindowsApplication1. In the WindowsApplication1 I create a simple usercontrol: UserControl1: [code] public class UserControl1 : System.Windows.Forms.UserControl, WindowsLibrary1.ITest { .... public void Test() { // implementation of the WindowsLibrary1.ITest.Test() } } [/code] Ofcourse all compiles fine, but I can not use this usercontol at all. Whenever I try to drop the control on a form I get the following error message: --------------------------- Microsoft Development Environment --------------------------- The user control 'WindowsApplication3.UserControl1' could not be loaded. Ensure that the library containing the control has been built and a project reference has been made to the library containing the control. If you have changed the name of the user control, close and re-open the control's designer to update the toolbox item. --------------------------- OK --------------------------- I badly need this to make it work, so any help is greatly appreciated. Thanks, Corneliu. PS>> Environment: VS.Net 2003, WinXP.
Sounds like a version problem. With .NET assemblies and the CLR, version numbers are VERY important (unlike version numbers with native DLLs, which are basically there for reference and for decent installers so they don't copy over newer versions). When you work with multiple projects in a solution, you really should not use automatic versioning, i.e. using an asterisk in the
[assembly:AssemblyVersionAttribute]
, typically found in your AssemblyInfo.cs file. The problem seems to be that the version your first project is referencing references one version while the component you added to the toolbox is of a different version. One other way you can help this is to use a project reference instead of an assembly refernece. When you add a reference to your project (right click, Add Reference...), click on the Project tab and add the project that you want to reference. This establishes a build dependency (making sure that any changes in the dependency prompt a rebuild so the referencing assembly has the latest code) and make sure that the assemblies that are referenced are the correct version. As far as the toolbox goes, don't explicitly add components to it that you develop and are currently developing in the open solution. Third-party controls and controls that you've build stables version of and deployed should be used there. VS.NET - though not all the time - will automatically add a reference to anyUserControl
that you open in the VS.NET designer to the toolbox. If it doesn't, switch to code view, define your field, and instantiate it in theInitializeComponent
method that the designer uses. This will make sure it can be designed if you need that sort of thing. You can then continue setting properties and adding event handlers like you normally would on a control using the designer.Microsoft MVP, Visual C# My Articles
-
Sounds like a version problem. With .NET assemblies and the CLR, version numbers are VERY important (unlike version numbers with native DLLs, which are basically there for reference and for decent installers so they don't copy over newer versions). When you work with multiple projects in a solution, you really should not use automatic versioning, i.e. using an asterisk in the
[assembly:AssemblyVersionAttribute]
, typically found in your AssemblyInfo.cs file. The problem seems to be that the version your first project is referencing references one version while the component you added to the toolbox is of a different version. One other way you can help this is to use a project reference instead of an assembly refernece. When you add a reference to your project (right click, Add Reference...), click on the Project tab and add the project that you want to reference. This establishes a build dependency (making sure that any changes in the dependency prompt a rebuild so the referencing assembly has the latest code) and make sure that the assemblies that are referenced are the correct version. As far as the toolbox goes, don't explicitly add components to it that you develop and are currently developing in the open solution. Third-party controls and controls that you've build stables version of and deployed should be used there. VS.NET - though not all the time - will automatically add a reference to anyUserControl
that you open in the VS.NET designer to the toolbox. If it doesn't, switch to code view, define your field, and instantiate it in theInitializeComponent
method that the designer uses. This will make sure it can be designed if you need that sort of thing. You can then continue setting properties and adding event handlers like you normally would on a control using the designer.Microsoft MVP, Visual C# My Articles
Thanks for the answer. Unfortunatelly it does not help as none of your recomandations apply. I've already tried most of them before posting the question. I have the feeling that something has happend with my VS.Net after I've un-intalled some addin (I think it was ReShape or something like that). Since then VS behaves sometimes strange. I've just discovered that my example works perfectly on another sistem, thus I think I'll spend my day with reinstalling VS.Net with the hope that this will solve my problem. Thanks alot, Corneliu.