difference between winforms and User Controls

E

Eric

I was hoping someone could explain what are the differences between a
winform and a user control besides the fact that a user control is designed
to be hosted in a winform. But you can also host a winform inside another
winform by setting the toplevel property and adding it to the controls
property. Given this fact and that a user control is derived from the form
class itself, it seems that the user control class isn't really needed. I'm
interested in any comments or suggestions about when to one or the other.

Thanks,

Eric
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Eric,

First let me check that we are on the same page.
Windows Forms (or WinForms if you prefer) is the Microsoft's framework for
building UI applications with .NET. I assume you are asking about the
difference between UserControl and Form classes and respectively their
instances. Otherwise the answer would be easy - "They are totaly different
things"

Speeking of the Form and UserControl class there are some differences. They
are mostly regarding of what those controls (the form is control as well) is
meant to be used for. And here a little correction - UserControls dose not
inherits from Form class. They both inherit from the same base class though.

So the Form is meant to be used as a top level window. In this case the
underlying Windows window :) has WS_OVERLAPPEDWINDOW style applied. The user
control at the other hand is a child window (WS_CHILDWINDOW). Because of
this they behave differently (at least the form in this case can have main
menu; the user control cannot).

However once you set TopLevel property to *false* it changes the form style
to WS_CHILDWINDOW and from this point on the form is not much different then
any other control. It looks differently just because it has some window
styles applied (caption, thick border, etc, but it cannot have a main menu)
which the rest of the controls don't. But this is not something that you
cannot add to the user control.

Of course there are some other minor differences in the way one works with
such a form. For example one has to call form's show method in order the
form to become visible on the screen. But those are things that come because
of the fact that the form is not meant to be used in this way.

So my suggestion is if you want to create a control use UserControls keep
forms for applications' main windows or dialgos. Event the designers are
build with that usage in mind. You don't have any advantages using forms as
usercontrols, but only inconvenience.

Those are my two cents.
 
E

Eric

Thanks for the quick reply.

You are correct about the user control not inheriting from the form class.
I guess I noticed that it was in the forms assembly and didn't look in the
help for the correct inherited class. But back to the main question, I
agree with you about using user controls as controls and not using forms as
controls. I just wish there was a little more distinction between the 2
methods.

Thanks again for the reply.

Eric


Stoitcho Goutsev (100) said:
Hi Eric,

First let me check that we are on the same page.
Windows Forms (or WinForms if you prefer) is the Microsoft's framework for
building UI applications with .NET. I assume you are asking about the
difference between UserControl and Form classes and respectively their
instances. Otherwise the answer would be easy - "They are totaly different
things"

Speeking of the Form and UserControl class there are some differences. They
are mostly regarding of what those controls (the form is control as well) is
meant to be used for. And here a little correction - UserControls dose not
inherits from Form class. They both inherit from the same base class though.

So the Form is meant to be used as a top level window. In this case the
underlying Windows window :) has WS_OVERLAPPEDWINDOW style applied. The user
control at the other hand is a child window (WS_CHILDWINDOW). Because of
this they behave differently (at least the form in this case can have main
menu; the user control cannot).

However once you set TopLevel property to *false* it changes the form style
to WS_CHILDWINDOW and from this point on the form is not much different then
any other control. It looks differently just because it has some window
styles applied (caption, thick border, etc, but it cannot have a main menu)
which the rest of the controls don't. But this is not something that you
cannot add to the user control.

Of course there are some other minor differences in the way one works with
such a form. For example one has to call form's show method in order the
form to become visible on the screen. But those are things that come because
of the fact that the form is not meant to be used in this way.

So my suggestion is if you want to create a control use UserControls keep
forms for applications' main windows or dialgos. Event the designers are
build with that usage in mind. You don't have any advantages using forms as
usercontrols, but only inconvenience.

Those are my two cents.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Eric said:
I was hoping someone could explain what are the differences between a
winform and a user control besides the fact that a user control is designed
to be hosted in a winform. But you can also host a winform inside another
winform by setting the toplevel property and adding it to the controls
property. Given this fact and that a user control is derived from the form
class itself, it seems that the user control class isn't really needed. I'm
interested in any comments or suggestions about when to one or the other.

Thanks,

Eric
 

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