3

I have a form with many tabs.

In order to not having a huge Form1.cs file, I would like to split the methods in several Form1_1.cs, Form1_2.cs etc. file.

I do not find what is the right way to do so. Can I repeat the same namespace and Form in several files? Do I need to repeat all the compliler directives like "using" or only the ones that I will use in that part?

Example:

using System.Data.OleDb;

namespace Mine
{
    public partial class Form1 : Form
    {
...

Any help or advice will be very welcome.

Thank you in advance. David

David
  • 31
  • 1
  • 3
  • 1
    You could use [UserControls](http://msdn.microsoft.com/en-us/library/aa302342.aspx) for each tab, so you can delegate your code for each tab on separate classes. – Gregoire D. Jul 04 '13 at 14:38
  • Please refer to [#2477848](http://stackoverflow.com/a/2477848/11963) to see why splitting your form in even more partial classes might be a bad idea. – hangy Jul 04 '13 at 14:40
  • The `partial` modifier is meant for exactly this kind of situation, where you want to split a single class into multiple files. – Nolonar Jul 04 '13 at 14:41

1 Answers1

6

You already use partial class declaration. You can create several files with

namespace Mine
{
    public partial class Form1

declaration. Only the namespaces used in a particular file need to be used. See the details here: http://msdn.microsoft.com/en-us/library/vstudio/wa80x488(v=vs.120).aspx

ElDog
  • 1,230
  • 1
  • 10
  • 21