Can I create a custom control with a designer like a form designer

T

trant

Using Visual Studio 2008, is there anyway I can create a custom control which
is comprised of several standard windows controls put together in a certain
way?

For example, I want to have a panel, and inside this panel will be a few
other Panels with other controls within these panels. I want to position
them, set their docking and create a basic interface which would be grouped
together as one component.

Then I would want to go back to my main app and be able to add these
components throughout my app - just drag n drop in on some location set it's
docking and be done, so I dont have to recreate the whole component
repetively every time I need it.

Now, the standard "Designer" I get for my custom controls as far as I can
tell only allows me to drag n drop controls into a screen but it does not
render them and let me resize them and place other controls within them. For
example, I drag a Panel over and I just get some icon for the panel and it
says "panel1" but I cannot size it, position it etc...

How can I do this?
 
P

Peter Duniho

trant said:
Using Visual Studio 2008, is there anyway I can create a custom control which
is comprised of several standard windows controls put together in a certain
way? [...]

I'm not entirely sure I understand your description. The last paragraph
is a bit confusing. But based on everything else you wrote, it seems to
me that you probably just need to use a UserControl-derived class. Just
use the "Add Item..." command for the project, and select "User Control"
(it's in the same place you'd add a new form, just a different item).

The UserControl class is a lot like a Form, in the way it works in the
Designer. The main difference is that when compiled, it will then show
up in the Toolbox, from which you can drag the control to get a new
instance into a form, just like any other control type.

(You can actually also compile a UserControl into a DLL for reuse in
other projects. When you add the DLL containing the UserControl as a
reference for the other project, the UserControl then becomes available
in the Toolbox as noted above).

Pete
 
T

trant

Hi Peter,

Thank you for the quick response!


:

The UserControl class is a lot like a Form, in the way it works in the
Designer. The main difference is that when compiled, it will then show
up in the Toolbox, from which you can drag the control to get a new
instance into a form, just like any other control type.

Yes, this is exactly the way I am creating my custom Control. However the
thing is the designer for it is nothing like the designer for a Form. It does
not give me a window or panel to drag and drop other Controls into.

If I drag something like a Panel from my Toolbox over to the designer area
it just drops an icon representing the Panel, it does not actually render the
panel and allow me to resize it like a Form Designer does. As a result of
this, I cannot then add say a TextBox onto that panel, position it, resize
it, etc...

So using this Control Designer I cannot group together a bunch of Controls
into one like I was hoping to.

Is that not possible to do?

In my particular case, I want to have a panel which has a large area on the
right occupied by a ListView and a panel on the left with several drop down
controls inside. These drop down controls are used for interacting with data
from the List View. Altogether these Controls in this one panel act as one
single component which I would need multiple instances of across my
application. So I was hoping I could create some Control that wraps this all
up in one package making it easy to just drag n drop wherever I need it.

Neither "Control" nor "Component" seem to allow this. They allow me to drag
n drop controls from my Toolbox but they dont allow me to position them in in
a form-like view.
 
P

Peter Duniho

trant said:
The UserControl class is a lot like a Form, in the way it works in the
Designer. The main difference is that when compiled, it will then show
up in the Toolbox, from which you can drag the control to get a new
instance into a form, just like any other control type.

Yes, this is exactly the way I am creating my custom Control. However the
thing is the designer for it is nothing like the designer for a Form. It does
not give me a window or panel to drag and drop other Controls into.

If I drag something like a Panel from my Toolbox over to the designer area
it just drops an icon representing the Panel, it does not actually render the
panel and allow me to resize it like a Form Designer does. [...]

Assuming you are actually doing exactly what I said, it normally works
fine, just as you seem to expect it to.

Given that assmption, then unfortunately (for you, not me :) ) I have
not run into the problem you describe, so nothing comes to mind with
respect to fixing it. If you can post a concise-but-complete code
example that can successfully reproduce the same problem on someone
else's computer, then there's probably something that got messed up
about the code at some point.

If having the full complement of .cs files for the project isn't
sufficient for someone else, like me, to reproduce the problem, then you
probably have some kind of problem with your Visual Studio installation.
Unfortunately, the easiest way to fix that sort of thing is to
uninstall and reinstall from scratch (and depending on how messed up
things are, sometimes that won't even work).
In my particular case, I want to have a panel which has a large area on the
right occupied by a ListView and a panel on the left with several drop down
controls inside. These drop down controls are used for interacting with data
from the List View. Altogether these Controls in this one panel act as one
single component which I would need multiple instances of across my
application. So I was hoping I could create some Control that wraps this all
up in one package making it easy to just drag n drop wherever I need it.

Neither "Control" nor "Component" seem to allow this. They allow me to drag
n drop controls from my Toolbox but they dont allow me to position them in in
a form-like view.

If the base class of your custom control is "Control" or "Component",
then you have not inserted a new UserControl sub-class into your project
in the way that I described, and that would almost certainly lead to
exactly the behavior you are describing.

So before you start fiddling around with your Visual Studio
installation, or putting together and posting a code example, you should
double-check to make sure that you are really inserting a "User Control"
into your project, as opposed to a "Custom Control". Note that if you
right-click on the project in the Solution Explorer, under the "Add..."
menu item, there is a "User Control..." item that is a shortcut for
going through the whole template selection dialog.

Pete
 
T

trant

Peter,

Forgive me for it appears I have not followed your instructions.

I did not pick up on the distinction between User Control and Custom Control.

Now that I have added the *right* one, it functions exactly as I desired an
your described

Thank you!
 
P

Peter Duniho

trant said:
Peter,

Forgive me for it appears I have not followed your instructions.

I did not pick up on the distinction between User Control and Custom Control.

Now that I have added the *right* one, it functions exactly as I desired an
your described

Excellent...glad you got it working!
 

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