views:

979

answers:

4

Hi,

I engadged a problem with inherited Controls in WinForms, and need some advice on it.

I do use a base class for items in a List (selfmade GUI list made of a panel) and some inherited controls that are for each type of data that could be added to the list.

There was no problem with it, but I know found out, that it would be right, to make the base-control an abstract class, since it has methods, that need to be implemented in all inherited controls, called from the code inside the base-control, but must not and can not be implemented in the base class.

When I mark the base-control as abstract, the VS2008 Designer refuses to load the window.

Is there any way to get the Designer work with the base-control made abstract?

A: 

The Windows Form designer creating an instance of the base class of your form/control and apply the parse result of InitializeComponent. That's why you can design the form created by the project wizard without even building the project. Because of behavior you also can not design a control derived from an abstract class.

You can implement those abstract methods and throw exception when it is not running in designer. The programmer who derive from the control must provide an implementation that do not call your base class implementation otherwise the program would crash.

Sheng Jiang 蒋晟
pity, but thats how it is done yet. Hoped of a correct way to do this.
BeowulfOF
There is a better way, see Smelch's answer
Allen
+8  A: 
Smelch
Do your form and the abstract base class have a no-arg constructor ? Cause that's all we had to add to get the designer working to show a form that inherited from an abstract form.
nos
A: 

@Smelch, thanks for the helpful answer, as I was running into the same issue recently.

A minor change to your post to prevent compilation warnings (for some reason I can't enter a comment).

public class Form1
#if DEBUG  
 : MiddleClass 
#else  
 : BaseForm 
#endif 
Dave Clemmer
A: 

As BeowulfOF said: "...since it has methods, that need to be implemented in all inherited controls, called from the code inside the base-control, but must not and can not be implemented in the base class."

@Smelch: But then it's first at RELEASE build time that I will "discover" that Form1 needs to implement the abstract methods (since during DEBUG - where I tend to do my work - the MiddleClass will handle the mandatory overriding.) Hmm. Not the best of worlds, but still a well explained and clever solution (to a stupid VS-problem). However I think I will go with the exception suggestion of Sheng Jiang. Thanks all!

ua1