none
How do I stop c# code files from becoming form files?

    Question

  • I have a Visual Studio 2010 C# Windows Form project.  I have added a number of C# code files to the project.  Some of these files are being converted, after building or re-opening the project, into Windows Forms files - i.e. in the Solution explorer the thumbnail picture by the file name changes and they get a designer view with a blank form.  How do I convert them back to just a code file?  And how do I keep this from happening?

    RSBeech


    PS - these converted files do not get an Expand/Collapse control (which shows the <name>.Designer.cs and <name>.resx files), but their thumbnail is changed and if I double-click them they bring up a blank form designer rather than the code.
    • Edited by RBeech Friday, April 15, 2016 9:00 PM additional information
    Friday, April 15, 2016 8:56 PM

Answers

  • Lake,

    I think that I have found the problem - this behavior appears to result because these files are extensions of the main form of the project, i.e. "public partial class Form1" (see code snippet in my previous post).  If I change it to "public partial class NewClass" then they become just code files again - but, their functions are no longer part of the Form1 class.

    Is there a different way to split functions of the Form1 class across multiple files that doesn't make each file appear to be a form (now that I know why they appear this way, it isn't a problem, but I'm curious to know if I'm not doing this correctly)?


    RSBeech

    • Marked as answer by RBeech Thursday, April 21, 2016 10:47 AM
    Tuesday, April 19, 2016 11:25 AM

All replies

  • Hi RBeech,

    Thanks for your post.

    >>Some of these files are being converted

    Could you please tell me the detailed information about this converted files? If possible, please share us one so that we could reproduce it in our sides.

    Please try to right click the file in solution and click view code.

    As far as I know, some converted files can not be open in VS successfully because there might exist some compatibility issues.

    So one idea is that you'd better create a new windows form app and copy code instead of copying files into it.

    Best Regards,

    Lake Xiao


    Monday, April 18, 2016 3:09 AM
    Moderator
  • Lake,

    Yes, if I right click and choose view code in the Solution Explorer then I can open the code rather than the blank form - but these files shouldn't have a form at all.  I have included a screen capture of the Solution Explorer.  See, for example, the thumb nail for file "IniFileFuntions.cs" vs the file "Program.cs".  I created IniFileFunctions.cs by adding a new "code" file to the project, and it had the same thumb nail as Program.cs.  When I re-opened the solution the next day, the thumb nail had changed and a blank form is now associated with the file.  I have also included the opening lines of code from IniFileFunctions.cs, in case there is something there that you recognize that would explain this behavior.

    using System;
    using System.Windows.Forms;
    using System.IO;
    
    namespace KLARewrite
    {
        public partial class Form1
        {
            const int NumberOfParameters = 12;
    
            //*********************************
            //Load in parameters from the .ini file
            private bool ReadIniFile()
            {
                String LineText, LineLabel, ParameterString;
                int ColonIndex, j;
                bool[] ParameterFound = new bool[NumberOfParameters];


    RSBeech


    PS - Just to be clear, I am not "converting files" and putting them into the solution/project.  I am adding files within the project by right-clicking the project and choosing Add-New Item and then picking the C# code option.  Later, it is VS that converts the file from a code file to a form file.
    • Edited by RBeech Monday, April 18, 2016 12:30 PM additional clarification
    Monday, April 18, 2016 12:25 PM
  • Hi RSBeech,

    Thanks for your response.

    Please try running Visual Studio in Safe mode to Check whether some add-ins and extensions affect it(devenv /safemode).

    >>I am adding files within the project by right-clicking the project and choosing Add-New Item and then picking the C# code option.  Later, it is VS that converts the file from a code file to a form file.

    How about creating a new windows form app then adding a new item? Will it convert to the form file later?

    If so, I suppose there might be something wrong with your Visual Studio 2010. Try to repair your Visual Studio then create a new windows form app, try it again.

    In addition, if possible, please use VS2015 Community instead of using VS2010. I did a test in my VS2015, it works well and doesn't have this strange behavior.

    Best Regards,

    Lake Xiao

    Tuesday, April 19, 2016 1:08 AM
    Moderator
  • Lake,

    I think that I have found the problem - this behavior appears to result because these files are extensions of the main form of the project, i.e. "public partial class Form1" (see code snippet in my previous post).  If I change it to "public partial class NewClass" then they become just code files again - but, their functions are no longer part of the Form1 class.

    Is there a different way to split functions of the Form1 class across multiple files that doesn't make each file appear to be a form (now that I know why they appear this way, it isn't a problem, but I'm curious to know if I'm not doing this correctly)?


    RSBeech

    • Marked as answer by RBeech Thursday, April 21, 2016 10:47 AM
    Tuesday, April 19, 2016 11:25 AM
  • Hi  RSBeech,

    >>Is there a different way to split functions of the Form1 class across multiple files that doesn't make each file appear to be a form

    As far as I know, you can write a separate class inherited from the form class. Then add all the related functions into it.

    Best Regards,

    Lake Xiao

    Wednesday, April 20, 2016 1:17 AM
    Moderator
  • Lake,

    Thanks for the input.  What does "...inherited from the form class." mean?

    Russ


    RSBeech

    Wednesday, April 20, 2016 1:27 AM
  • Hi Russ,

    Something like this:

    In addition, for some other questions about windows form. I suggest you'd better post a new thread in the windows form forum for better support.

    Thanks for your understanding.

    Best Regards,

    Lake Xiao

    Wednesday, April 20, 2016 9:11 AM
    Moderator