16

I have a Window Form App project. At the moment all of my code is in Form1.cs file which is the default file. Now I have about 1300 lines of code in this single file. I want to break down this one file code into several files and I want to use the "partial" key word (I don't want to do anything drastic). So how should I add the files

Right click project name->add->new item ->class results into class1.cs, class2.cs and so on

But this file converts to a form form file after compilation. What's the correct way of adding so that the new file integrates with my existing project Form1.cs and Form1.cs[Design]?

default
  • 11,485
  • 9
  • 66
  • 102
user1903439
  • 1,951
  • 7
  • 19
  • 29
  • 4
    `partial` has its uses, but I'd suggest what might be suitable is to break the code down in to logical units, creating separate classes to be used within the form, instead of spanning the form across many files. – Grant Thomas Feb 19 '13 at 08:47
  • You can name the files whatever you want, just make sure the namespace and class name are identical and both are marked `partial`. – John Willemse Feb 19 '13 at 08:48
  • Are you adding a "Form" file or a "C# class"? – default Feb 19 '13 at 08:50
  • 1
    I am adding a C# class. But after adding it converts to Form file when I name newly the added class as **Form1.newclass.cs** – user1903439 Feb 19 '13 at 09:12
  • Have you ever found any way to make this work? I'm facing the exact same issue atm. The file shows as windows form and doesn't go below the main form name. – Vinz Jun 20 '16 at 12:37
  • You may want to take a look at [here](https://stackoverflow.com/questions/20796628/partial-form-class-c-sharp-only-display-code-view-for-class) and [there](https://stackoverflow.com/questions/33303004/is-it-possible-use-c-sharp-partial-winform-class-files-without-seeing-a-form-ico). – Mehdi Sep 13 '20 at 11:36

2 Answers2

10

You have to keep the namespace, the class name and mark it with partial. The file name is not really important for it to work, but it's a good practice so that the developers can identify rapidly the contents of the file.

Form1.cs

namespace TheSameNamespace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }

    // other definitions
}

Form1.Designer.cs

namespace TheSameNamespace
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        // the rest of the designer class
    }
}

Form1.Calculations.cs

namespace TheSameNamespace
{
    partial class Form1
    {
        // calculation methods definitions
    }
}

Form1.EventHandlers.cs

namespace TheSameNamespace
{
    partial class Form1
    {
        // event handlers definitions
    }
}

and so on...

Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • 1
    ..., however, it's usually a good idea to give the file a useful name (like `Form1.Calculations.cs` and `Form1.EventHandlers.cs` and so on), according to its contents. – Nuffin Feb 19 '13 at 08:51
  • 1
    ... however, this is almost always unnecessary and avoidable. Partiality is excellent for separating generated- from user-code, but abusing it just to compartmentalise your logic should be reconsidered. Hell, I'd prefer over-#regionated code than this. – Grant Thomas Feb 19 '13 at 09:10
  • 1
    Answer is correct. But one thing is left out. In the project file you need to add : Form1.cs – JRB Jul 03 '19 at 13:57
1

The partial keyword is primarly for generated files, which can be extended by your own code - there is no use in splitting a single bloated class into multiple partials, but if you really want to do it then you have to:

  1. Create a new class.
  2. Rename the class to match your own class (Form1.xxx.cs)
  3. Use the partial key-word and adjust the name and the namespace.

To clearify:

Form1.cs

public partial class Form1 { /* ... */ }

Form1.somepart.cs

public partial class Form1 { /* ... */ }
TGlatzer
  • 5,815
  • 2
  • 25
  • 46