Question on Controls and components

R

RBrowning1958

here's an easy one for you guys - .NET form designer generates this:

private System.ComponentModel.IContainer components = null;

but I don't see it used anywhere in the generated code - what's all
that about? Also, it generates code to add the individual controls to
Controls as in:

this.Controls.Add(this.comboBox1);
this.Controls.Add(this.menuStrip1);

What's all this about?

Ta

Ray
 
A

Andy

here's an easy one for you guys - .NET form designer generates this:

private System.ComponentModel.IContainer components = null;

but I don't see it used anywhere in the generated code - what's all
that about?

There are non-visual units of code called Components that can be
modified in the designer. These will appear in the Component tray of
the form designer. Try adding a BackgroundWorker component to a form;
its not a visual control, but you can still set it up via the
designer.
Also, it generates code to add the individual controls to
Controls as in:

this.Controls.Add(this.comboBox1);
this.Controls.Add(this.menuStrip1);

The designer isn't magic; its just a code generation tool. if you
didn't have the designer to work with, that's how you would create
controls and place them on the form. this refers to the Form instance
and it has a Controls collection. The form needs to know about the
controls which are placed on it for various reasons, and the control
needs to know what is containing it. That's all that code is doing.
If you fail to have those lines, you'll never see the control, but it
doesn't know what it should be painting itself on.

HTH
Andy
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
here's an easy one for you guys - .NET form designer generates this:

private System.ComponentModel.IContainer components = null;

This line most of the time looks like that and has no use, no idea why it's
still generated.
but I don't see it used anywhere in the generated code - what's all
that about? Also, it generates code to add the individual controls to
Controls as in:

this.Controls.Add(this.comboBox1);
this.Controls.Add(this.menuStrip1);

That code is NEEDED, it does include the controls inside the Controls
collection (or either the form or to another container control) If you do
not do that the control is not displayed ni the screen.
 
R

RBrowning1958

There are non-visual units of code called Components that can be
modified in the designer. These will appear in the Component tray of
the form designer. Try adding a BackgroundWorker component to a form;
its not a visual control, but you can still set it up via the
designer.



The designer isn't magic; its just a code generation tool. if you
didn't have the designer to work with, that's how you would create
controls and place them on the form. this refers to the Form instance
and it has a Controls collection. The form needs to know about the
controls which are placed on it for various reasons, and the control
needs to know what is containing it. That's all that code is doing.
If you fail to have those lines, you'll never see the control, but it
doesn't know what it should be painting itself on.

HTH
Andy

Thanks for the reply. I know about compoents - if I add them to the
form they are not added to the components container tho....the
Controls thing appears to be for children. Ta Ray
 
R

RBrowning1958

Hi,

--
Ignacio Machinhttp://www.laceupsolutions.com




This line most of the time looks like that and has no use, no idea why it's
still generated.



That code is NEEDED, it does include the controls inside the Controls
collection (or either the form or to another container control) If you do
not do that the control is not displayed ni the screen.

Thanks for this reply. Controls appears to be used for controls'
children - if you add a group box foe ex. and add controls to the
groupbox then they are added to the groupbox's controls. All clear on
that. U say that Components is not used - and I haven't sen it used
either. Other systems (such as Delphi) also keep a list of owned
components - things that need to be dsestroyed - I would have thought
componets would be used for that but there's no concept of ownership
in the framewok is there 'cos of GC I suspect. Ta Ray
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Thanks for this reply. Controls appears to be used for controls'
children - if you add a group box foe ex. and add controls to the
groupbox then they are added to the groupbox's controls. All clear on
that. U say that Components is not used - and I haven't sen it used
either. Other systems (such as Delphi) also keep a list of owned
components - things that need to be dsestroyed - I would have thought
componets would be used for that but there's no concept of ownership
in the framewok is there 'cos of GC I suspect. Ta Ray

I think that the need to having a list of Components is cause they implement
IDisposable, so the Form's dispose can call all the dispose methods of these
components.
 
R

RBrowning1958

Hi,

--
Ignacio Machinhttp://www.laceupsolutions.com
Mobile & warehouse Solutions.


I think that the need to having a list of Components is cause they implement
IDisposable, so the Form's dispose can call all the dispose methods of these
components.

HI there - I see how the Dispose method uses components - and your
suggestion sounds correct in that it's to with impleemnting Dispose -
I'm just not sure how things get added to it?

Ta

Ray
 
R

RBrowning1958

Hi,

--
Ignacio Machinhttp://www.laceupsolutions.com




This line most of the time looks like that and has no use, no idea why it's
still generated.



That code is NEEDED, it does include the controls inside the Controls
collection (or either the form or to another container control) If you do
not do that the control is not displayed ni the screen.

<<This line most of the time looks like that and has no use, no idea
why it's
still generated.>>
Just figured this out - it's to do with compoents with a specific type
of consturctor - add an imagelist to the form for example and it's
used.
 

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