Components added to a Components Surface not in Components Collection?

M

Microsoft

I've created a Component called MyComponent. To the design surface of
MyComponent I've dragged an SqlConnection object, an SqlCommand object, and
an SqlDataAdapter object and linked these items together appropriately.

My question is this: Why are these Sql components not added to the list of
components contained by MyComponent? (These objects are not added to the
private "components" member of MyComponent.) Is there a technical reason
for this or is it just a bug in the designer? Is there any way I can get
the Designer to add these items to the collection automatically, without
having to hand-code it myself?

Thanks,
-Dave
 
P

Peter Huang

Hi Dave,

Thanks for posting in the community.
My question is this: Why are these Sql components not added to the list of
components contained by MyComponent? (These objects are not added to the
private "components" member of MyComponent.)
This is because the Sql components did not implement the contructor as
below.
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
Me.New()
Container.Add(Me)
End Sub

For detailed information, you may take a look at the article below.
What's that "Windows Form Designer generated code" anyway?
http://www.vbinfozine.com/a_disposable_comp.shtml

Is there any way I can get
the Designer to add these items to the collection automatically, without
having to hand-code it myself?
Since the SQLDataAdapter is an sealed class, it can not be derived, I think
we need to do it by coding ourselves.

As for the other question, I am doing researching, if I have any new
information, I will update you with ASAP.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Microsoft

Thanks for the input. The reason I ask this question is that at run-time I
am simply trying to initialize my SqlConnection object to supply it with the
connection string that is known at run time as well as handling messages
returned from the InfoMessage event. I understand there are many ways of
doing this, but I'm trying to automate the process within designer as much
as possible.

My goal was to create a ConnectionHandler component that would implement the
ISupportInitialize interface. Within the ISupportInitialize.EndInit() method
for this component, I wanted to iterate through the list of components
hosted by my container object, find the SqlConnection object, and then do my
initializations. The result (if this had worked) would be that my container
component (whether it was a Windows Form, ASP.NET Page, or another
Component), could be programmed almost completely through the designer and
have the run-time functionality I desired. The problem is, obviously, that
my SqlConnection object is never added to the container object's list of
hosted components and thus my ConnectionHandler object can't find it.

Of course, if you allocate the SqlConnection object yourself within the
component constructor, it is simple enough to add to the list of components,
but of course you lose functionality within the designer, such as editing
properties or viewing a DataSet. This is because the SqlConnection object
is no longer on the design surface of the container component.

Also, adding the SqlConnection (or any other contained object, for that
matter) after the call to InitializeComponent() has finished, you lose the
ability to take advantage of the ISupportInitialize() interface. When it's
time to add the component to the collection, the EndInit() method has
already been called on the contained components.

I'm probably making a mountain out of a molehill here. I've got plenty of
ways to work around this limitation. However, it would have been nice for
the designer to properly add any Component-derived objects to its collection
automatically.

-Dave
 

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