none
Can't reference inherited user control from within same project where the control is defined (VS2010 C#)

    Question

  • I've found this question asked several times of the past years, but so far none of the answers have fixed my problem...

    1) In VS2010, I create a new inherited user control definition in my existing C# Winform project (Right-click project, select Add User Control).  I let the name default to UserControl1,  then open the .cs code-behind and change it to inherit FlowLayoutPanel.

    2) I build the project and get the expected error about the AutoScaleMode property. I delete the line in UserControl1.Designer.cs which references AutoScaleMode and it then builds OK.

    3) At this point, UserControl1 appears in my app's Components section of the ToolBox (when I have the apps' MainForm selected).  I can now drag the control to the MainForm of my app, resize it with the VS Designer and configure it via the Properties window.

    4) But the app will not build. I get the error: The type name 'UserControl1' does not exist in the type 'MyNameSpace.MyAppName'  (I've edited the error message to conceal the actual namespace, which is confidential). 

    Obviously, the app can't find the UserControl1 class that is (or should be) part of the app's assembly! The UserControl1.cs file's Build Action is set to Compile. And I can see UserControl1 in the Class View (under the same namespace as my app),

    What am I missing?

    Thanks,

    DC

     

     

     

     



    • Edited by dthouston Thursday, May 26, 2011 9:00 PM another typo
    Thursday, May 26, 2011 8:58 PM

Answers

  • They're thinking that the DigitalClock in this statement, this.digitalClock1 = new DigitalClock.DigitalClock();, is your custom class DigitalClock, instead of your project's namespace name. You can just rename the DigitalClock UserControl name by "F2" in the Solution Explorer, it will change the first DigitalClock to the new name, instead of the second DigitalClock string.

    The same as your other statements, which also will let IDE think the first DigitalClock is a type instead of the namespace.

    So, please use the different class name to the namespace, and it is better to avoid use a name which existing in other places(namespace name, built in class name, built in namespace name and so on).


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 22, 2011 3:42 AM
    Moderator

All replies

  • Hi dthouston,

    I think it is better to check each of the classes' namespace, maybe they really in the different namespace.

    Since, I made a simple demo according yours description, the project works fine for me in my local side.

    If you also cannot let it work in your pc system, have you tried test it in another pc system?

    Otherwise, please upload your visual studio solutions to the "sky driver", and then share the download link to us in your post.

     

    If there's any concern, please feel free to let us know.

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 27, 2011 7:03 AM
    Moderator
  • I changed the line in MainForm.Designer.cs which was causing the build error.

    As-generated, that line was:

       this.userControl11 = new MyNameSpace.UserControl1();

    I changed it - removing the namespace - so it reads:

       this.userControl11 = new UserControl1();

     ... and it now builds.
     
    However, I still don't know why the designer was inserting the namespace in that line.  Hopefully, it won't re-insert it again!

    DC

    • Edited by dthouston Friday, May 27, 2011 1:23 PM Bugs in the editor
    Friday, May 27, 2011 1:16 PM
  • VS2010 continues to insert the namespace in the MainForm.Designer.cs file each time it updates the file.

    So my workaround is too painful.

    How can I fix the problem permanently?

    (uploading my code isn't an option)

    DC

     

    Friday, May 27, 2011 3:04 PM
  • Hi,

    you can't stop VS from referencing types by their full name in the designer generated code, and as you can see, changing manually generated code is not really an option unless you almost never change the design of the form.

    If you can't upload your project try creating a simple project and reproduce the behavior you described. You might figure out what's happening in the process. I failed to reproduce it based on the steps you mentioned.

    Also, you might wanna try removing that control from the toolbox, cleaning the solution and rebuilding it (try closing & reopening VS before rebuild).

    BTW, have you been changing build configuration in the process ?

    Best regards,
    Vladimir

    Saturday, May 28, 2011 12:42 AM
  • As we mentioned that we cannot repro this issue as you describe successfully.

    So, please upload your visual studio solution and let us clear about if there's any settings in your visual studio solution.

    Otherwise, we cannot ensure if it is a visual studio issue or your visual studio solution issue just from your description above. And since we don't think so this is a visual studio issue.

    If it is a visual studio issue, then you can ref the following thread solve it:

    http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/b74860da-9aa7-45eb-9a12-50c161ec1f89

     

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, May 30, 2011 6:53 AM
    Moderator
  • Hi dthouston,
    I am writing to check the status of the issue on your side. 
    What about this problem now? 
    Would you mind letting us know the result of the suggestions?
    Best wishes,

    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 02, 2011 6:36 AM
    Moderator
  • Hey Mike,

    I have the same problem as dthousten and have included my solution in a rar file on skydrive Any help is appreciated.

    https://skydrive.live.com/redir.aspx?cid=328e96a0073f95e8&resid=328E96A0073F95E8!108


    Tuesday, August 09, 2011 3:19 PM
  • I had a similar problem with a VB.Net project.  In my case the problem was that the Root namespace matched the name of one of my classes.
    Wednesday, September 21, 2011 3:25 PM
  • They're thinking that the DigitalClock in this statement, this.digitalClock1 = new DigitalClock.DigitalClock();, is your custom class DigitalClock, instead of your project's namespace name. You can just rename the DigitalClock UserControl name by "F2" in the Solution Explorer, it will change the first DigitalClock to the new name, instead of the second DigitalClock string.

    The same as your other statements, which also will let IDE think the first DigitalClock is a type instead of the namespace.

    So, please use the different class name to the namespace, and it is better to avoid use a name which existing in other places(namespace name, built in class name, built in namespace name and so on).


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 22, 2011 3:42 AM
    Moderator