views:

136

answers:

4

I didn't realize at the time I create this particular application that I'd need to reuse some of the components - some Windows forms and a class or two.

Now that I've already created the fairly complex forms inside one project, what's the easiest way to transform those forms into inheritable forms that I can reuse in other projects? Once that's done I'd like to modify the existing project to use the newly created inheritable forms.

How can I accomplish this with as little pain as possible? I'm using C# in Visual Studio 2008.

+1  A: 

Just declare an empty class that inherits from System.Windows.Forms.Form and then make your "huge" class inherit from that. Once that works, start moving, a small reusable piece at a time from your "huge" class to the parent class.

hova
I think I sort of see what you're saying here, but it's not very clear.
MusiGenesis
+2  A: 

You don't really have to do anything special to achieve this. Your form is already inheritable. On any new form, just make sure the first line looks like this:

public partial class frmMyChild : frmMyInheritableForm

instead of:

public partial class frmMyChild : Form

and make any methods that you need to access from the child either "public" or "protected".

Update: one additional trick is to set the Modifiers property of each control on your original form to Protected (instead of the default Private). In the designer for your child form that inherits from this form, you will then see all of the controls on the parent form, and you can move them and resize them as you see fit (this will not affect the original form's layout).

To access any parent method from the child form, you just call:

base.MyMethod();
MusiGenesis
This works great. How can I override a method on the parent form?
Dave
Use the "new" keyword on the child method of the same name. Example: if your parent has a void method named "DoSomething()", you declare a method in the child form "public new void DoSomething()" (this is called "hiding" rather than "overriding"). You can still call base.DoSomething() inside the child DoSomething() method, in case you want to do some other stuff and then call the base method.
MusiGenesis
@Musi: Why would you advocate hiding (an arguably "evil" practice) rather than declaring the methods as virtual?
Adam Robinson
@Adam: genocide is an evil practice. I'm assuming Dave isn't going to go crazy with multiple levels of form inheritance, so in this case the difference is pretty insignificant.
MusiGenesis
+1  A: 

In your first project, add a new "Windows Forms Control Library" to your solution

Drag the windows/classes from the original project to the new one.

fix the errors.

At this point, you now have a Class Library which you can include in your second windows project.

Stephen Wrighton
@Stephen - I've created a new Windows Forms Control Library" to my existing project and dragged (moved) a form to it. How do I access it from the existing project now?
Dave
add a reference to the NEW project to the old one, and then where you did declare it as "myFormClass myForm = new myFormClass();" you would now use "myClassLibrary.myFormClass myForm = new myFormClass();"
Stephen Wrighton
+1  A: 

Bear in mind that you don't need to design your forms to be inheritable in order to use them in other projects. Inheritable forms are a PITA, and in all but the simplest circumstances are more trouble than they're worth.

If you're simply looking to design your forms to be more portable, then the biggest thing that would be required is ensuring that you do NOT expose internal fields (Controls are included in that) outside of the form. If outside code (be it in the same or another project) needs to interact with the form in some visual or behavioral way, then you need to expose functions and properties that represent that functionality, rather than the control itself.

Apart from the design of the particular form, it would likely be helpful (if a somewhat time-consuming exercise) to move these common forms into a separate control library. While you can definitely add your .exe as a reference to another project, that's not ideal (and not entirely intuitive).

Adam Robinson
What was PITA-like in your experience with inheritable forms? I've used them quite a bit and never had any real problems with them.
MusiGenesis
They may have gotten better, but I used them in VS 2002/2003 and they were, indeed, a pain. Redrawing was unpredictable, and sometimes on-form controls would just plain disappear out of the blue. I think things have gotten better in later versions of VS. One thing that helps is to make sure that you always have a parameterless constructor, so the designer can call it. If you don't plan to call it (you'll only call constructors with parameters), make the parameterless one private, but don't delete it or the designer will give you trouble.
Kyralessa