Designer Error

Jarod

Regular
Joined
Feb 5, 2003
Messages
82
Location
Bruxelles
In some case, I want to make my own custom control. And in some cases, I want to have first an abstract class to group global code like :
Visual Basic:
public mustInherit Class myTextBoxBase
   inherits TextBox

...
end class

public Class myTextBox1
   inherits myTextBoxBase
...
end class

public Class myTextBox2
   inherits myTextBoxBase
...
end class
With that structure, my classes work fine but I have a designer error while looking in myTextBox1 and myTextBox2 designer because my base class is abstract and so cannot be built.
This can be annoying if I want to add other graphic component in a SubClass.

Is there a way of avoiding that error (and letting the base class as abstract?)

Thanks,
 
What error do you get, and at exactly what point is it thrown? When you try to create your controls on the form?
 
The error is when I try to see the designer of my Object (at design time)
The error is:
"An error occurred while loading the document. Fix that error and try loading the document again. The error message follows: The designer must create an instant of type 'myTextBoxBase' but it can't because the type is declared as abstract"

It is not a critical error because I can add my new components directly in the code instead of in the designer, but I just wonder if there is a way to have an abstract base class (for a component) and being able to work in the subclass designer
 
I have had no problems doing this in the designer. I just tried deriving two controls from my own class which inherits from UserControl, and I can create both of them in the designer just fine. It shouldn't be trying to create an instance of the base class, it should be trying to create an instance of one of your derived ones.

Do your derived classes have a parameterless constructor the designers can call?
 
And when it create an instance of the derived class, it has to create an instance of the base class. (well, not really an instance, but you have the constructor chaining) That's the normal process for inheritance.
And here the base class is "MustInherit" and so can't be instantiated.

In your test, Have you put your base class (inheriting from UserControl) as MustInherit ?
 
The problem doesn't appear when you add the Custom control to a form for example, but when you open the designer of the custom control
 
Oh, I see what you mean now. No, you can't use the designer to design a control that inherits from an abstract class.
 
You might try wrapping the abstract declaration with "#if !DEBUG" and use "#else" for the non-abstract version. This way, in DEBUG mode you can use the designer because your form is not abstract, and when you compile in Release it is. You may need to change it to abstract while in Debug mode for some extra testing (since making it abstract may cause problems if you didn't code it right).

Here's some C# code - I don't know the VB.NET syntax for "#if DEBUG" or which keywords make the declaration abstract (hopefully someone can translate this):
C#:
#if !DEBUG
	public abstract class Form1 : System.Windows.Forms.Form
#else
	public class Form1 : System.Windows.Forms.Form
#endif

-Nerseus
 
Back
Top