terrible annoying BUG in VS.NET 2005 with UserControl at DesignTime?!

P

Pieter

Hi,

I'm getting a really terrible and anoying bug/problem in VS.NET 2005:

1. Create a new Windows Application.
2. Add a new class Class1.
3. Add a usercontrol UserControl1.
4. Add a public instance of Class1 to UserControl1 (with WithEvents):
"Public WithEvents ThisClass As New Class1".
5. Build the Solution, and put an instance of UserControl1 on Form1.
6. Rebuild the Solution, and open the Form1 in the Designer (View Designer)
-> Everything works fine, you can close it and open it again in the designer
as much as you want...

7. Change anything to Form1 in the Designer: for instance make it just a
tiny bit larger. Save the changes, and close the Form1.
-> When you nowwant to open Form1 in the Designer, you get some nasty
exception:
*****************
One or more errors encountered while loading the designer. The errors are
listed below. Some errors can be fixed by rebuilding your project, while
others may require code changes.

The type 'WindowsApplication1.UserControl1' has no field named 'ThisClass'.
Hide Edit

at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager
manager, String exceptionText, String helpLink)
at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager
manager, CodeAssignStatement statement)
at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager
manager, CodeStatement statement)

****************

I noticed it added in the Form1.Designer.vb some lines in the
InitializeComponent():
"Dim Class11 As WindowsApplication1.Class1 = New WindowsApplication1.Class1"
and a little bit further "Me.UserControl11.ThisClass = Class11".
You can't open Form1 in the Designer if you don't remove the second line...

This behaviour doesn't happen if you declare the instance ThisClass as
Private, or without the WithEvents...

But I really would like to have it Public WithEvents. And I can't stand it
that I can't, hehe. Does anybody knows a solution for this? Is this a Bug?
Or is there a rule that a UserControl can't containt Public WithEvents
classes??

Any help, hints, or whatever would be really appreciated!


Thanks a lot in advance,

Pieter
 
A

Armin Zingler

Pieter said:
Hi,

I'm getting a really terrible and anoying bug/problem in VS.NET
2005:

1. Create a new Windows Application.
2. Add a new class Class1.
3. Add a usercontrol UserControl1.
4. Add a public instance of Class1 to UserControl1 (with
WithEvents): "Public WithEvents ThisClass As New Class1".
5. Build the Solution, and put an instance of UserControl1 on Form1.
6. Rebuild the Solution, and open the Form1 in the Designer (View
Designer) -> Everything works fine, you can close it and open it
again in the designer as much as you want...

7. Change anything to Form1 in the Designer: for instance make it
just a tiny bit larger. Save the changes, and close the Form1.
-> When you nowwant to open Form1 in the Designer, you get some
nasty exception:
*****************
[...]


I can repro the problem (VB 2005 Express). I get the error even the first
time I open the Form after the 2nd build. Really seems to be a bug.
(However, I think the best group for this question is one of the
microsoft.public.vstudio groups, because it's an IDE (designer) issue, thus
I left the framework groups out.)


Armin
 
J

JasonS

Hi,

I'm getting a really terrible and anoying bug/problem in VS.NET 2005:

1. Create a new Windows Application.
2. Add a new class Class1.
3. Add a usercontrol UserControl1.
4. Add a public instance of Class1 to UserControl1 (with WithEvents):
"Public WithEvents ThisClass As New Class1".
5. Build the Solution, and put an instance of UserControl1 on Form1.
6. Rebuild the Solution, and open the Form1 in the Designer (View Designer)
-> Everything works fine, you can close it and open it again in the designer
as much as you want...

7. Change anything to Form1 in the Designer: for instance make it just a
tiny bit larger. Save the changes, and close the Form1.
-> When you nowwant to open Form1 in the Designer, you get some nasty
exception:
*****************
One or more errors encountered while loading the designer. The errors are
listed below. Some errors can be fixed by rebuilding your project, while
others may require code changes.

The type 'WindowsApplication1.UserControl1' has no field named 'ThisClass'.
Hide Edit

at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager
manager, String exceptionText, String helpLink)
at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager
manager, CodeAssignStatement statement)
at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager
manager, CodeStatement statement)

****************

I noticed it added in the Form1.Designer.vb some lines in the
InitializeComponent():
"Dim Class11 As WindowsApplication1.Class1 = New WindowsApplication1.Class1"
and a little bit further "Me.UserControl11.ThisClass = Class11".
You can't open Form1 in the Designer if you don't remove the second line...

This behaviour doesn't happen if you declare the instance ThisClass as
Private, or without the WithEvents...

But I really would like to have it Public WithEvents. And I can't stand it
that I can't, hehe. Does anybody knows a solution for this? Is this a Bug?
Or is there a rule that a UserControl can't containt Public WithEvents
classes??

Any help, hints, or whatever would be really appreciated!


Thanks a lot in advance,

Pieter

Well, it does appear to be a bug or confusion by the designer for VB.Net,
but it's also a rather messy way of doing things.

A more correct way to implement this type of feature is to use something
like:
Public Class UserControl1
Private _ThisClass As New Class1()
Public ReadOnly Property ThisClass() As Class1
Get
Return _ThisClass
End Get
End Property
End Class

There is no point in having WithEvents on the declaration unless you plan
to handle the events in the usercontrol itself. If so, just add WithEvents
to the definition of _ThisClass and add 'Handles _ThisClass.xxx' in your
UserControl.
If you want to intercept Class1's events from outside of the UserControl
then you have to use something like:

Private Sub Form1_Load(...) Handles MyBase.Load
AddHandler (Me.UserControl11.ThisClass).TestClick, AddressOf doit
:
:

Private Sub doit()
MessageBox.Show("Hi!")
End Sub

but it's better to add your own events to the UserControl and ripple them
down from the embedded object.

The reason you have to wrap the Class1 object in a ReadOnly property is to
prevent it being serialized and initialised in the Form1.Designer.vb class.

Cheers,
Jason
 
J

JasonS

Hi,

I'm getting a really terrible and anoying bug/problem in VS.NET 2005:

1. Create a new Windows Application.
2. Add a new class Class1.
3. Add a usercontrol UserControl1.
4. Add a public instance of Class1 to UserControl1 (with WithEvents):
"Public WithEvents ThisClass As New Class1".
5. Build the Solution, and put an instance of UserControl1 on Form1.
6. Rebuild the Solution, and open the Form1 in the Designer (View Designer)
-> Everything works fine, you can close it and open it again in the designer
as much as you want...

7. Change anything to Form1 in the Designer: for instance make it just a
tiny bit larger. Save the changes, and close the Form1.
-> When you nowwant to open Form1 in the Designer, you get some nasty
exception:
*****************
One or more errors encountered while loading the designer. The errors are
listed below. Some errors can be fixed by rebuilding your project, while
others may require code changes.

The type 'WindowsApplication1.UserControl1' has no field named 'ThisClass'.
Hide Edit

at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager
manager, String exceptionText, String helpLink)
at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager
manager, CodeAssignStatement statement)
at
System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager
manager, CodeStatement statement)

****************

I noticed it added in the Form1.Designer.vb some lines in the
InitializeComponent():
"Dim Class11 As WindowsApplication1.Class1 = New WindowsApplication1.Class1"
and a little bit further "Me.UserControl11.ThisClass = Class11".
You can't open Form1 in the Designer if you don't remove the second line...

This behaviour doesn't happen if you declare the instance ThisClass as
Private, or without the WithEvents...

But I really would like to have it Public WithEvents. And I can't stand it
that I can't, hehe. Does anybody knows a solution for this? Is this a Bug?
Or is there a rule that a UserControl can't containt Public WithEvents
classes??

Any help, hints, or whatever would be really appreciated!


Thanks a lot in advance,

Pieter

Well, it does appear to be a bug or confusion by the designer for VB.Net,
but it's also a rather messy way of doing things.

A more correct way to implement this type of feature is to use something
like:
Public Class UserControl1
Private _ThisClass As New Class1()
Public ReadOnly Property ThisClass() As Class1
Get
Return _ThisClass
End Get
End Property
End Class

There is no point in having WithEvents on the declaration unless you plan
to handle the events in the usercontrol itself. If so, just add WithEvents
to the definition of _ThisClass and add 'Handles _ThisClass.xxx' in your
UserControl.
If you want to intercept Class1's events from outside of the UserControl
then you have to use something like:

Private Sub Form1_Load(...) Handles MyBase.Load
AddHandler (Me.UserControl11.ThisClass).TestClick, AddressOf doit
:
:

Private Sub doit()
MessageBox.Show("Hi!")
End Sub

but it's better to add your own events to the UserControl and ripple them
down from the embedded object.

The reason you have to wrap the Class1 object in a ReadOnly property is to
prevent it being serialized and initialised in the Form1.Designer.vb class.

Cheers,
Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top