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 files without the usual editor support, all...
To split the c# main code file is simple by the looks of it

public partial class Form1
{
}

The code i wrote in the first post appears not to be wrong, so the project file corruption problem and run errors i am having is caused by something else.

The project file creates a design window, a form1 for every partial file you have on any VS other than the one in current use, you do not have to run it. If one of the windows/partial files are closed on a "working" project to open it again in the solution explorer brings up a design window with your partial file. The new design window/form1 created with each of your partial files conflicts with the projects original. What could be the cause?

some information found on the net that may help others:

public partial class Form1 : Form By using partial it is possible to write the definition of same class in two different source file in the same namespace.It will be treated as same during compilation.You can find a class with same name Form1 in your project which is created automatically.

Form1 is the name of the Form and : is used to inherit the properties of base class . Here Form represents System.Windows.Forms.Form. We are inheriting to access the properties and methods of base class.
 
Last edited:
How and when is the PicBox() method invoked by the Form1 class? It shouldn't matter if PicBox() lives in Form1.cs or in PicBox.Form1.cs. If it works while it lives in Form1.cs, it should continue to work in PicBox.Form1.cs. If it doesn't even work in Form1.cs, then it doesn't matter if it got moved to PicBox.Form1.cs.
 
How and when is the PicBox() method invoked by the Form1 class? It shouldn't matter if PicBox() lives in Form1.cs or in PicBox.Form1.cs. If it works while it lives in Form1.cs, it should continue to work in PicBox.Form1.cs. If it doesn't even work in Form1.cs, then it doesn't matter if it got moved to PicBox.Form1.cs.
Skydiver the code is not called it is clearly described in the text what the code shows you lol, it says it is not actual working code, it shows you what i was asking for, not to demonstrate a working program it is all clearly written in the post, ya gotta laugh huh? I can make it work, there is no point as i say it demonstrates how i wanted to move code to a separate file, to make the code neater ect (all has been described already) in the most simplistic way i can. I could move on and go through the rest of your comments but they are covered in the post you have read and my last post shows i have already answered my own question anyway LOL making it even more pointless, i am bored so this forum does provide a little entertainment. Cheers anyways, any idea whats up with VS as described in my last post?
 
The compiler doesn't care what files are in the project, and they will be compiled if included in the projects configuration, providing your code matches up as it was wrote.
Anyway, why are you trying to adapt your C++ habits in a C# app?
 
The compiler doesn't care what files are in the project, and they will be compiled if included in the projects configuration, providing your code matches up as it was wrote.
Anyway, why are you trying to adapt your C++ habits in a C# app?
Sheepings microsoft have put the partial file feature into c# and they have made this available for the main form code, i want to use this feature that microsoft have put in becouse it makes code neater for me, i like it and it is easy to use when you know how. I do not like C++, it is a over complicated mess in my opinion which is why i use c# it is far from perfect but a better option. I am not trying to bring any c++ habits to c#. Maybe you don`t like (hate?) the partial file feature? You don`t have to use it you know, just don`t answer threads on this subject....leave it mate. There is no need to answer anything i say...just leave it mate.

Anyway hi there,

I asked how do split up c# main code file, well, by finding the correct keywords and using google the truth is out there.

You split up the main code with:

public partial class Form1

Now here is the other bit you need to know:

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 now leaves the run errors if they still remain. There will be no point in posting anything here, it is a waste of time i now expect no usefull answer whatsoever because that is what i have received.

This thread is done no more posts thanks.
 
Last edited:
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.
 
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
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.
 
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();
 
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.
 
The way to solve this is to add, in the secondary source file declaring the partial form class, a litle dummy class before the partial form class.

This tricks to Visual Studio Form Designer declaring a multi class file instead the only single class as the designer expects.
You can double-click the source file in the solution explorer as a normal source file.

No .RESX nor strange files wil be created.


MyForm.Extend.cs
namespace MyApp.Forms
{
#region Dummy
// ADDING this dummy declaration
public partial class DummyClass
{
}
#region Dummy

public partial class MyForm
{
// MORE Methods, propeties , ....
}

}
 
Back
Top Bottom