Calling a User Control

  • Thread starter Thread starter Patrick De Ridder
  • Start date Start date
P

Patrick De Ridder

If I create a new and separate User Control, in the same way as I
would create a new Form with the IDE, and call it from the main form,
like so:

UserControl1 usc = new UserControl1();
usc.Show();

nothing happens. What is it, that I should do instead?
 
AFAIK, a control must be placed onto, or assigned to, a form in order to be
displayed. (I believe this is what Michael Culley was saying.) The Show()
/ Hide() methods merely make the control visible or not on the form.

--
Mike Mayer
http://www.mag37.com/csharp/
(e-mail address removed)


Michael Culley said:
I think this should do it.

this.controls.add (usc);
 
AFAIK, a control must be placed onto, or assigned to, a form in order to be
displayed. (I believe this is what Michael Culley was saying.) The Show()
/ Hide() methods merely make the control visible or not on the form.

Thanks,

Do I understand correctly that you are saying that a Control can only
be placed on an existing Form? (I.e., you have the Form in the
Designer and you drag a Control onto it).

My question then is: What then, is the function for, in the IDE, that
enables you to create a stand-alone Control, if a Control cannot live
a separate life?
 
Patrick De Ridder said:
Do I understand correctly that you are saying that a Control can only
be placed on an existing Form? (I.e., you have the Form in the
Designer and you drag a Control onto it).

That's what I'm saying. I've only written a few controls, and am not an
expert on them, but my understanding is that a control's purpose in life is
to be added to a form (windows and/or web).

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsforms.htm

Note that you don't strictly _have_ to drag a control onto a form. Code
such as Michael Culley showed should work too:
this.controls.add (usc);
or perhaps more like this:
Form aForm = new Form();
aForm.Controls.Add(new MyControl());
My question then is: What then, is the function for, in the IDE, that
enables you to create a stand-alone Control, if a Control cannot live
a separate life?

I'm not entirely sure what you mean by stand-alone control. Do you mean a
"User Control"? Those are generally used for grouping other controls into
one unit that you can re-use. For instance, you may want a control with a
label, a textbox, and a button that you can use again and again on forms to
provide that combined functionality of the three other controls, without
having to explictly place each of the three. There are many other, much
more powerful uses.

If you've made a control you want to show by itself, you could probably put
it on a form with no title bar, no border, and make the control fill the
entire area.

Hope that helps clear up some issues. I may be wrong on some of this, as
this is only my understanding. I haven't been able to find anything in the
documentation to support this (only hints such as the above ms-help link).
 
If you've made a control you want to show by itself, you could probably put
it on a form with no title bar, no border, and make the control fill the
entire area.

Thanks. Yes I have already done that for a project that is completed
now. Before I received help from this ng on how to do boarderless
Forms, someone suggested using "stand-alone" User Controls, hence a
further question, for me to understand how "stand-alone" User Controls
should be used.
 
. . . someone suggested using "stand-alone" User Controls, hence a
further question, for me to understand how "stand-alone" User Controls
should be used.

I have found sufficient information in MSDN.
 
Back
Top