none
Problem building user control in form RRS feed

  • Question

  • Using Visual Studio 2008, C#, .Net 3.5 - code upgraded from VS 2003, .Net 1.1

    I have a class library contain a bunch of forms.  It builds and runs fine.

    I then add a new user control in the same project.  I can build the project and the control shows up in the toolbox.  I use that to add the user control to one of the forms.  Now when I build the project, the new user control will not build with it.  I get an error message saying it "does not exist".  If I take the user control off the form (but is still part of the project), everything is fine again.  I can add other windows controls and that will build and run ok.

    Any ideas as to why this is happening?

    I've tried deleting and reestablishing all references, cleaning the solution, rebooting and even completely deleting the project and reloading it from source control.

    Thanks.

    Monday, March 9, 2009 7:05 PM

Answers

  • Well, the designer is confused about the namespace in which MyUserControl is declared.  What is the correct namespace?
    Hans Passant.
    Wednesday, March 11, 2009 7:44 PM
    Moderator
  • Hi again,

    Yes, the designer was confused.  Your last post finally led me to figure out the problem.

    Here was the issue and I was able to reproduce it in a new project....
     
    In my class library named (per my example) MyClassLib, I created the new user control MyUserControl.  However, there was also a form named MyClassLib.  When the designer added the new user control to a different form, it inserted it as MyClassLib.MyUserControl and the designer was complaining that it was looking for and couldn't find a type under the (form) class MyClassLib instead of MyUserControl within the namespace MyClassLib. 

    Which explains why it then worked fine if I changed "new MyClassLib.MyUserControl()" to just "new MyUserControl()"

    So, the moral of the story is - use unique names everywhere.  (which I know seems obvious and I want to add that I didn't write the original code - I inherited the code this way and now surprised this didn't come up sooner since this isn't the only case where it happens)


    Thanks for your help.

    • Marked as answer by rm88 Thursday, March 12, 2009 7:55 AM
    • Unmarked as answer by rm88 Thursday, March 12, 2009 7:56 AM
    • Marked as answer by Jing0Moderator Tuesday, March 17, 2009 7:46 AM
    Thursday, March 12, 2009 7:55 AM
  • Hi,

     

    ->this.uControl = new MyClassLib.MyUserControl();

     

    If you add MyClassLib before, the designer will confused because there are two MyClassLib: a form and a control.

    If we don’t add MyClassLib before, this will find MyUserControl method in the class and all reference, and there is only one MyUserControl method.

    Use partial modifier, and then they can use the same name.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, March 16, 2009 7:46 AM
    Moderator

All replies

  • Quote the exact error message.
    Hans Passant.
    Tuesday, March 10, 2009 1:02 AM
    Moderator
  • See below for the error message on this problem.

    But before you go there, I've discovered some other weird behavior:

    When I drag the control from the tool box onto the form, the control is rendered.  As stated above, when I try to run my application, I get the error that the control is not defined (and the designer shows the error below).

    When I  look at the code, I see the following was added when I added the new user control:

        private MyUserControl uControl;

    and in InitializeComponent()

        this.uControl = new MyClassLib.MyUserControl();  <-- Visual studio is showing the error here;

    If I remove "MyClassLib" so that this line is:   this.uControl = new MyUserControl();
    everything compiles and runs correctly.

    If I add another instance of this control, this line reverts back to "this.uControl = new MyClassLib.MyUserControl(); " when I do another build.

    and again if I remove "MyClassLib" from all occurrences, everything compiiles and appears to be running properly.


    So, I have a "solution" but it would be nice to know why this is happening and how to truly fix whatever is causing this behavior.



    ======== the error message:
    I'm not sure if this is what you're requesting....
    error displayed under "Show Call Stack"
     at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
    at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
    at System.ComponentModel.Design.Serialization.CodeDomSerializer.DeserializeStatementToInstance(IDesignerSerializationManager manager, CodeStatement statement)
    at System.ComponentModel.Design.Serialization.CodeDomSerializer.Deserialize(IDesignerSerializationManager manager, Object codeObject)
    at System.Windows.Forms.Design.ControlCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, Object codeObject)
    at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.DeserializeName(IDesignerSerializationManager manager, String name, CodeStatementCollection statements)

    Wednesday, March 11, 2009 6:57 PM
  • Well, the designer is confused about the namespace in which MyUserControl is declared.  What is the correct namespace?
    Hans Passant.
    Wednesday, March 11, 2009 7:44 PM
    Moderator
  • Hi again,

    Yes, the designer was confused.  Your last post finally led me to figure out the problem.

    Here was the issue and I was able to reproduce it in a new project....
     
    In my class library named (per my example) MyClassLib, I created the new user control MyUserControl.  However, there was also a form named MyClassLib.  When the designer added the new user control to a different form, it inserted it as MyClassLib.MyUserControl and the designer was complaining that it was looking for and couldn't find a type under the (form) class MyClassLib instead of MyUserControl within the namespace MyClassLib. 

    Which explains why it then worked fine if I changed "new MyClassLib.MyUserControl()" to just "new MyUserControl()"

    So, the moral of the story is - use unique names everywhere.  (which I know seems obvious and I want to add that I didn't write the original code - I inherited the code this way and now surprised this didn't come up sooner since this isn't the only case where it happens)


    Thanks for your help.

    • Marked as answer by rm88 Thursday, March 12, 2009 7:55 AM
    • Unmarked as answer by rm88 Thursday, March 12, 2009 7:56 AM
    • Marked as answer by Jing0Moderator Tuesday, March 17, 2009 7:46 AM
    Thursday, March 12, 2009 7:55 AM
  • Hi,

     

    ->this.uControl = new MyClassLib.MyUserControl();

     

    If you add MyClassLib before, the designer will confused because there are two MyClassLib: a form and a control.

    If we don’t add MyClassLib before, this will find MyUserControl method in the class and all reference, and there is only one MyUserControl method.

    Use partial modifier, and then they can use the same name.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, March 16, 2009 7:46 AM
    Moderator