Windows Develop Bookmark and Share   
 index > Windows Forms General > VS 2008 SP1 abstract Form/UserControl not working even when using Brian Pepin, Urban Potato, technique. Help...
 

VS 2008 SP1 abstract Form/UserControl not working even when using Brian Pepin, Urban Potato, technique. Help...

Hello everyone,

I'm using Visual Studio 2008 SP1 and I'm following this article:

http://www.urbanpotato.net/default.aspx/document/2001

So I created a new WindowsFormsApplication project. I added this code to the Form1.cs file

1 namespaceWindowsFormsApplication70
2 {
3 [TypeDescriptionProvider(typeof(ConcreteClassProvider))]
4 publicabstractpartialclassForm1:Form
5 {
6 publicForm1()
7 {
8 InitializeComponent();
9 }
10
11 publicabstractstringAbstractProperty{get;set;}
12 }
13
14 //HereisaconcreteversionofAbstractFormthatactsasastandin
15 internalclassConcreteForm:Form1
16 {
17 string_abstractProperty;
18
19 publicoverridestringAbstractProperty
20 {
21 get{return_abstractProperty;}
22 set{_abstractProperty=value;}
23 }
24 }
25
26 //Hereisourtypedescriptionprovider.Allourproviderneedsto
27 //doisreturnConcreteFormasthereflectiontype.
28 internalclassConcreteClassProvider:TypeDescriptionProvider
29 {
30
31 //BecauseweonlywanttoaugmentthemetadataforAbstractForm,insteadof
32 //completelyreplaceit,wepassintothebaseclassthecurrenttype
33 //descriptionprovider.Thisistheproviderthatnormallyhandles
34 //metadataforAbstractForm.Bydoingthisallwehavetodois
35 //overridetheareaswewanttochange.
36 publicConcreteClassProvider():base(TypeDescriptor.GetProvider(typeof(Form1))){}
37
38 //Tellanyonewhoreflectsonusthattheconcreteformisthe
39 //formtoreflectagainst,nottheabstractform.Thisway,the
40 //designerdoesnotseeanabstractclass.
41 publicoverrideTypeGetReflectionType(TypeobjectType,objectinstance)
42 {
43 if(objectType==typeof(Form1))
44 {
45 returntypeof(ConcreteForm);
46 }
47
48 returnbase.GetReflectionType(objectType,instance);
49 }
50
51
52 //IfthedesignertriestocreateaninstanceofAbstractForm,weoverride
53 //itheretocreateaconcereteforminstead.
54 publicoverrideobjectCreateInstance(IServiceProviderprovider,TypeobjectType,Type[]argTypes,object[]args)
55 {
56 if(objectType==typeof(Form1))
57 {
58 objectType=typeof(ConcreteForm);
59 }
60
61 returnbase.CreateInstance(provider,objectType,argTypes,args);
62 }
63 }
64 }

I build the solution and everything works fine. Then I added another Form called Form2 and this is what I have on that Form2.cs file:

1 namespaceWindowsFormsApplication70
2 {
3 publicpartialclassForm2:Form1
4 {
5 privatestring_AbstractProperty;
6
7 publicForm2()
8 {
9 InitializeComponent();
10 }
11
12 publicoverridestringAbstractProperty
13 {
14 get
15 {
16 returnthis._AbstractProperty;
17 }
18 set
19 {
20 this._AbstractProperty=value;
21 }
22 }
23 }
24 }

All pretty basic as you can see.... When I try to open Form2 in the Designer I get the usual error:

The designer must create an instance of type 'WindowsFormsApplication70.Form1' but it cannot because the type is declared as abstract.

Any ideas why is the designer still giving me this error? Did I miss something?

Thanks a lot in advance
Federico Silberberg  Saturday, February 21, 2009 12:33 AM
Yes, the usual error. The designer doesn't support abstract base classes.

Here's a useful tidbit: before you get to number 71, use Tools + Options, Project and Solutions, General, turn off "Save new projects when created".

Hans Passant.
nobugz  Saturday, February 21, 2009 1:49 AM
That wasn't meant as a workaround for this issue at all, just a workaround for having 70 projects named WindowsFormsApplicationXx. As far as I can tell, if there ever was a plan to use [TypeDescriptionProvider] in the designer to allow substituting a base class, that plan never survived the Beta 1 version of Whidbey (VS2005). The author of the article acknowledges as much. I don't see it getting used at all.

Hans Passant.
nobugz  Tuesday, February 24, 2009 9:04 PM
nobugz said:

That wasn't meant as a workaround for this issue at all, just a workaround for having 70 projects named WindowsFormsApplicationXx.


Well I don't have a solution with 70 projects called WindowsFormsApplicationXxx. I just have a Test folder which has many WindowsFormsApplicationXxx solutions so no need for a workaround on that....

But what you provided believe it or not did work as a workaround even though is not working 100% well....

If anybody else is interested, you can findanother workaround forthis in here:

http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/ddbc181f-dda4-48ba-94b3-194494c6b1e1/
Federico Silberberg  Wednesday, February 25, 2009 2:36 AM
Yes, the usual error. The designer doesn't support abstract base classes.

Here's a useful tidbit: before you get to number 71, use Tools + Options, Project and Solutions, General, turn off "Save new projects when created".

Hans Passant.
nobugz  Saturday, February 21, 2009 1:49 AM
Thanks for getting back to me. With that tidbit I was able to make it work. The only problem is that every time I change anything I need to close the project and re-open it otherwise I get the usual error:

The designer must create an instance of type 'WindowsFormsApplication70.Form1' but it cannot because the type is declared as abstract.

Is there a way to get around this without having to close and re-open the project, etc?

Thanks
Federico Silberberg  Monday, February 23, 2009 3:46 PM
Federico Silberberg said:

Thanks for getting back to me. With that tidbit I was able to make it work. The only problem is that every time I change anything I need to close the project and re-open it otherwise I get the usual error:

The designer must create an instance of type 'WindowsFormsApplication70.Form1' but it cannot because the type is declared as abstract.

Is there a way to get around this without having to close and re-open the project, etc?

Thanks


Do you have multiple classes, forms, or controls within the same file? The Designer doesn't support multiple classes within the same file. It looks at the first class in the file, and ignores the rest.


Get in the habit of closing all files in a project, and then closing the project before you exit VS.

Mark the best replies as answers. "Fooling computers since 1971."
  • Edited byRudedog2 Monday, February 23, 2009 4:32 PMexit
  •  
Rudedog2  Monday, February 23, 2009 4:31 PM
Rudedog2 said:

Do you have multiple classes, forms, or controls within the same file?


No, I don't. I just have something very simple. Basically I'm trying to do this with UserControls. It gets very hard to develop like this if I need to close and re-open the project every time I doa small or big change.... Is there a better way out there?

Thanks
Federico Silberberg  Monday, February 23, 2009 8:34 PM
Federico Silberberg said:

Rudedog2 said:

Do you have multiple classes, forms, or controls within the same file?


No, I don't. I just have something very simple. Basically I'm trying to do this with UserControls. It gets very hard to develop like this if I need to close and re-open the project every time I doa small or big change.... Is there a better way out there?

Thanks

Are you still using that abstract form class?


Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Monday, February 23, 2009 9:05 PM
Rudedog2 said:

Are you still using that abstract form class?



Do you mean Form1? That example which I wrote above, basically it doesn't work properly.... Meaning if you add a variable to Form2 for example the designer will give you the exception no matter what until you close the Project and re-open it...
Federico Silberberg  Monday, February 23, 2009 11:34 PM
Federico Silberberg said:

Rudedog2 said:

Are you still using that abstract form class?



Do you mean Form1? That example which I wrote above, basically it doesn't work properly.... Meaning if you add a variable to Form2 for example the designer will give you the exception no matter what until you close the Project and re-open it...



Why do you think that doesn't it work properly?

classClass3
{
publicClass3()
{
return;
}
}
classClass3A
{
publicClass3A()
{
return;
}
}
publicclassTestClass
{
voidMethod1()
{
Class3Aobj=newClass3A();//setabreakpointontbisline!
}
}

Set a breakpoint on that line. Press F11 until execution returns to that line.

In order for the Form Designer to use your base class, it must create a concrete instance of it.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, February 24, 2009 12:52 AM
Rudedog2,

I don't understand what that code is suppose to tell me..... Where should I copy and paste that code first of all?

Have you take a look at the code I posted at the top of everything?

All I really want to know if there is a solution for this or not.....

Thanks
Federico Silberberg  Tuesday, February 24, 2009 2:32 PM
Federico Silberberg said:

Rudedog2,

I don't understand what that code is suppose to tell me..... Where should I copy and paste that code first of all?

Have you take a look at the code I posted at the top of everything?

All I really want to know if there is a solution for this or not.....

Thanks


That is not a solution to your problem. It was a thought problem. A demonstration of how inherited objects are created. A deomonstration of how a derived object contains an instance of the base class object.

You were already given a solution by nobugz. The solution to your problem is stop using the abstract base form class. I explicitly asked you if you were still using the abstract class as your base form. You ignored the question. I assume that means, "Yes, I am still using it."

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, February 24, 2009 3:02 PM
Federico Silberberg said:



The designer must create an instance of type 'WindowsFormsApplication70.Form1' but it cannot because the type is declared as abstract.



Thanks


nobugz said, "Yes, the usual error. The designer doesn't support abstract base classes. "

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, February 24, 2009 3:04 PM

Rudedog2,

I hope I don't sound rude butdid you read my firstpost on this thread? Did you goover the code?I honestlydon't think you have...

Federico Silberberg said:

When I try to open Form2 in the Designer I get the usual error:

The designer must create an instance of type 'WindowsFormsApplication70.Form1' but it cannot because the type is declared as abstract.

Rudedog2 said:

1 classClass3
2 {
3 publicClass3()
4 {
5 return;
6 }
7 }
8 classClass3A
9 {
10 publicClass3A()
11 {
12 return;
13 }
14 }
15 publicclassTestClass
16 {
17 voidMethod1()
18 {
19 Class3Aobj=newClass3A();//setabreakpointontbisline!
20 }
21 }
22
Federico Silberberg said:

Rudedog2,

I don't understand what that code is suppose to tell me.....

Rudedog2 said:

That is not a solution to your problem. It was a thought problem. A demonstration of how inherited objects are created. A deomonstration of how a derived object contains an instance of the base class object.

First I never said that was a solution to my problem and secondly I don't see any inheritationon that code either......

Rudedog2 said:

You were already given a solution by nobugz. The solution to your problem is stop using the abstract base form class.

That was not the solution given by nobugz. Thiswassome kind of workaround provided by nobugz:

nobugz said:
Here's a useful tidbit: before you get to number 71, use Tools + Options, Project and Solutions, General, turn off "Save new projects when created".

The problem is that workaround unfortunately doesn'talways work well.....

Rudedog2 said:

I explicitly asked you if you were still using the abstract class as your base form. You ignored the question. I assume that means, "Yes, I am still using it."

I ignored that question because is obvious that I still would like to use an abstract class. Otherwise I wouldn't keep asking if somebody knows any other solution/workaround......

Thanks

Federico Silberberg  Tuesday, February 24, 2009 7:39 PM
That wasn't meant as a workaround for this issue at all, just a workaround for having 70 projects named WindowsFormsApplicationXx. As far as I can tell, if there ever was a plan to use [TypeDescriptionProvider] in the designer to allow substituting a base class, that plan never survived the Beta 1 version of Whidbey (VS2005). The author of the article acknowledges as much. I don't see it getting used at all.

Hans Passant.
nobugz  Tuesday, February 24, 2009 9:04 PM
Federico Silberberg said:

Hello everyone,

I'm using Visual Studio 2008 SP1 and I'm following this article:

http://www.urbanpotato.net/default.aspx/document/2001

So I created a new WindowsFormsApplication project. I added this code to the Form1.cs file

1 namespaceWindowsFormsApplication70
2 {
3 [TypeDescriptionProvider(typeof(ConcreteClassProvider))]
4 publicabstractpartialclassForm1:Form
5 {
6 publicForm1()
7 {
8 InitializeComponent();
9 }
10
11 publicabstractstringAbstractProperty{get;set;}
12 }
13
14 //HereisaconcreteversionofAbstractFormthatactsasastandin
15 internalclassConcreteForm:Form1
16 {
17 string_abstractProperty;
18
19 publicoverridestringAbstractProperty
20 {
21 get{return_abstractProperty;}
22 set{_abstractProperty=value;}
23 }
24 }
25
26 //Hereisourtypedescriptionprovider.Allourproviderneedsto
27 //doisreturnConcreteFormasthereflectiontype.
28 internalclassConcreteClassProvider:TypeDescriptionProvider
29 {
30
31 //BecauseweonlywanttoaugmentthemetadataforAbstractForm,insteadof
32 //completelyreplaceit,wepassintothebaseclassthecurrenttype
33 //descriptionprovider.Thisistheproviderthatnormallyhandles
34 //metadataforAbstractForm.Bydoingthisallwehavetodois
35 //overridetheareaswewanttochange.
36 publicConcreteClassProvider():base(TypeDescriptor.GetProvider(typeof(Form1))){}
37
38 //Tellanyonewhoreflectsonusthattheconcreteformisthe
39 //formtoreflectagainst,nottheabstractform.Thisway,the
40 //designerdoesnotseeanabstractclass.
41 publicoverrideTypeGetReflectionType(TypeobjectType,objectinstance)
42 {
43 if(objectType==typeof(Form1))
44 {
45 returntypeof(ConcreteForm);
46 }
47
48 returnbase.GetReflectionType(objectType,instance);
49 }
50
51
52 //IfthedesignertriestocreateaninstanceofAbstractForm,weoverride
53 //itheretocreateaconcereteforminstead.
54 publicoverrideobjectCreateInstance(IServiceProviderprovider,TypeobjectType,Type[]argTypes,object[]args)
55 {
56 if(objectType==typeof(Form1))
57 {
58 objectType=typeof(ConcreteForm);
59 }
60
61 returnbase.CreateInstance(provider,objectType,argTypes,args);
62 }
63 }
64 }

I build the solution and everything works fine. Then I added another Form called Form2 and this is what I have on that Form2.cs file:

1 namespaceWindowsFormsApplication70
2 {
3 publicpartialclassForm2:Form1
4 {
5 privatestring_AbstractProperty;
6
7 publicForm2()
8 {
9 InitializeComponent();
10 }
11
12 publicoverridestringAbstractProperty
13 {
14 get
15 {
16 returnthis._AbstractProperty;
17 }
18 set
19 {
20 this._AbstractProperty=value;
21 }
22 }
23 }
24 }

All pretty basic as you can see.... When I try to open Form2 in the Designer I get the usual error:

The designer must create an instance of type 'WindowsFormsApplication70.Form1' but it cannot because the type is declared as abstract.

Any ideas why is the designer still giving me this error? Did I miss something?

Thanks a lot in advance



No inheritance from an abstract form class. Guess I was wrong.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, February 24, 2009 11:15 PM
nobugz said:

That wasn't meant as a workaround for this issue at all, just a workaround for having 70 projects named WindowsFormsApplicationXx.


Well I don't have a solution with 70 projects called WindowsFormsApplicationXxx. I just have a Test folder which has many WindowsFormsApplicationXxx solutions so no need for a workaround on that....

But what you provided believe it or not did work as a workaround even though is not working 100% well....

If anybody else is interested, you can findanother workaround forthis in here:

http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/ddbc181f-dda4-48ba-94b3-194494c6b1e1/
Federico Silberberg  Wednesday, February 25, 2009 2:36 AM

You can use google to search for other answers

Custom Search

More Threads

• Webbrowser urls to images
• CellFormatting Event problems
• Generic Undo Redo in an editor .
• Store data in a persistent way
• Determining if a form is the "active" form?
• Compilation Error
• Spooky things around HasChanged ???????
• Best approach to provide detailed feedback on errors?
• reading and operating on an input from a textbox
• Capturing Live video