Home > Enterprise >  Partial Class Winform Form
Partial Class Winform Form

Time:02-28

It was taught to me that any alterations to an object on a form (change the text, make visible/invisible or change the color and so on) should be done in the corresponding form class.

But due to the large amount of such alterations done inside the project, the file has became large and hard to search through. I've read online that a Partial Class could help, but there was no explanation on how to implement this. As an easy example, I have the following 2 files: Form_Main.cs

namespace Test
{
    partial class Form_Main : Form
    {
        public Form_Main()
        {
             InitializeComponent();    
        }
    }
}

AND Form_Main.Dataloader.cs

 namespace Test
    {
        partial class Form_Main : DataLoader
        {
            public void SetText()
            {
                TextBox_StudentSurname.Text = "1";        
            }
        }
    }

How can I make this work? Because if I do this I get several errors in the designer. enter image description here

CodePudding user response:

Your main problem is the first error printed: You cannot declare different base classes in different partial implementations. I don't know which of the two base classes is the right one (the one you previously used), but as always, a class cannot have two base classes. It is legal to specify the base class only in one of the parts, but if it is specified multiple times, it must be the same.

  • Related