Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Partial Class for a Form

I would like to create a partial class for my form. I have a lot of events, and it gets messy, so I would like to break up sections into their own files.

The problem: When I create a partial class of my form, say:

Partial Public Class Form1

End Class

Visual Studio decides I need another form for this partial class.

Questions:
1. How do I create a partial class for a form?
2. If I cant do that, how can I break up all the events in my form into different files?

like image 884
Connor Albright Avatar asked Feb 25 '23 17:02

Connor Albright


2 Answers

Yeah, it does. As soon as you drop a control on this phantom form, you'll get the design-time code (InitializeComponent) generated into that source code file. This is compatibility behavior for .NET 1.x, it didn't support the Partial keyword. Which will break the build, there are now two of them. It is somewhat avoidable with careful clicking, but you know it's going to happen sooner or later.

Other things go wrong too btw, the designer can no longer track an event handler when you move it from one file to another. And will readily let you add another, a much trickier source of bugs.

This just doesn't work very well, abandon hope of relying on it to solve your problem.

The generic diagnostic is that a convoluted user interface begets convoluted code. But that ship has sailed, no doubt. A more structural solution is pursuing the MVC model, separating the data from the view of the data. You'll still have a lot of event handlers but they won't do anything more than calling a method of a class that does the real work. Whose source code can of course live in another source code file. The typical hangup is that Windows Forms has no support whatsoever built in for this, you have to craft it by hand. Nothing similar to the MVVM model in WPF.

Something that can work well is isolating control + code into a separate UserControl. You have to do so carefully though, you don't want to have to add a bunch of properties and events that expose internal controls.

like image 189
Hans Passant Avatar answered Mar 06 '23 21:03

Hans Passant


Sometimes I create partial classes for better readibility, especially when I have very large classes. But when I click on the partial class, then the VS IDE will open the form editor showing me an empty form. If I do not care, than I could damage the main form (it seems to be a bug of VS 2008/2010)

A possibility could be using DesignerCategoryAttribute Class

Mark the partial class with the attribute "code".

 <System.ComponentModel.DesignerCategory("code")>
 Partial Class Form1

 End Class

In this way when you click on the file, you open the class in the code editor. Of course this will apply to all files, also to the main form file. If you want to edit again your form in the form editor, you have to quote the attribute:

 '<System.ComponentModel.DesignerCategory("code")>

Some more details here.

like image 26
gingo Avatar answered Mar 06 '23 20:03

gingo