Answered Split the main form into smaller files

MakeItWork

Member
Joined
Feb 20, 2021
Messages
6
Programming Experience
1-3
Hi all,

Hopefully this question is not to stupid so here we go.....
With c# window forms the main code starts with

public partial class Form1 : Form

I have been splitting the main code file into separate files that still compile into one with

public partial class Form1

It works but does not appear to like any other visual studio than the one it was set up in and the project file can corrupt easy.
I think it is the cause of other problems like Graphics .invalidate which has never worked. Windows size adjustment of the program being run does not change to the value it is set to. Int16 (short) conversion/use has caused errors with nothing wrong with the code. Picturebox code would not work (no compile errors) until it was moved to another location, nothing wrong with its original location and now graphics code doing the same.

I Googled the problem which gives no end on how to split classes, it is not clear how to split the main form.

How wrong is splitting code up using public partial class Form1 and what is the correct way.

Thanks in advance
 
Solution
As you can see in documentation inheritance and implemented interfaces are merged, you don't have to repeat this information in each file, and you can spread it across the files (see the article for example).

Don't try to design the partial files except the default one, or else designer will attempt to add designer code twice, which won't work. If you for example try to add a control to the extra partial class the designer adds InitializeComponent and more that conflicts.
Change the filename of added partial files to Form1.yourNameHere.cs

This stops the problem with a design window being attached to each added partial file.
Microsoft have in there wisdom made all added partial main code...

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,971
Location
UK
Programming Experience
10+
Here is the documentation for what you want to do : Partial Classes and Methods - C# Programming Guide

If you want to do it correctly, you will need to read how its done.

The separation of the same class into different files is something more common in C++.

A1.cs :
C#:
public partial class A1{}
A2.cs :
C#:
public partial class A1{}
Notice the file names are not the same but the class objects are named the same.

The files should be within the same namespace, and marked partial. With an instance of a class object, you could access both methods across multiple files. Read the docs above for more info and refrain from having hissy fits on the forum.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,068
Location
Norway
Programming Experience
10+
As you can see in documentation inheritance and implemented interfaces are merged, you don't have to repeat this information in each file, and you can spread it across the files (see the article for example).

Don't try to design the partial files except the default one, or else designer will attempt to add designer code twice, which won't work. If you for example try to add a control to the extra partial class the designer adds InitializeComponent and more that conflicts.
Change the filename of added partial files to Form1.yourNameHere.cs

This stops the problem with a design window being attached to each added partial file.
Microsoft have in there wisdom made all added partial main code files without the usual editor support, all the text remains white, so it is just store your finished code and compile, if you find it needs a edit just cut and paste whatever you need back, so no big problem.
That is not what I see. Since inheritance of all parts of the class is merged it will be possible to open designer for any part, but it will be empty.

It would be nice if the 'sub-parts' would display under the main class like the "Form1.Designer.cs" file in Solution Explorer, and opening designer would only open the main one, but VS doesn't support either.
There exist an extension File Nesting that at least can group the parts in Solution Explorer:
1614020241838.png


Code editor works fine with intellisense and code colors like normal when I test this.

I also tested moving event handlers from the default user code class to another, and surprisingly designer is fine with this, for example doubleclicking a control to get to default event handler automatically opens that method in the extra partial file.

Note that I have never added extra partial form class files before, I just did it now to test what works and not. If a class gets big I first look at refactoring stuff to other classes, or use regions for grouping.
 
Solution

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,971
Location
UK
Programming Experience
10+
Marked as best answer. You went out of your way on this one JohnH. Nicely explained too. (y)

Question though; did you find having the partial declaration : public partial class A1 in both files caused a problem, because it shouldn't, as far as I understand it. I'm just trying to understand that bit in your answer. Ie :
Don't try to design the partial files except the default one, or else designer will attempt to add designer code twice, which won't work.
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,971
Location
UK
Programming Experience
10+
Only one of the files should have the constructor/call to InitializeComponent();
Both files don't need this, and to my understanding they both need to use the same partial declaration class name within the two files. The designer code will only be called once providing only one of the files contains the following ex : InitializeComponent();
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,068
Location
Norway
Programming Experience
10+
It is possible to open designer for any of the partial files (since the class inherits Form), but designer won't see the generated code from default file, so it opens an empty designer and if you try to do something with that it will add designer generated code in that file too, which conflicts with the designer generated code in default file. So don't try to "design" the extra partial files. As I said, it would be nice if VS would just open the default designed form whichever partial class you opened in design view.
 
Top Bottom