Equivalent to Delphi TFrame?

T

Toby Groves

Is there an equivalent in Windows Forms to the Delphi TFrame, or
similar?

Basically I need a way of grouping together some controls in a re-usable
fashion so that I can embed them in various different forms as required.

There must be a way of doing this but I just can't see it.
 
T

Tim Wilson

Sounds like a UserControl. The UserControl will allow you to visually create
(with the help of the visual designer) a control that is composed of other
controls. Once you've created the custom UserControl you're free to add this
control to any container.
 
J

Jon Skeet [C# MVP]

Toby Groves said:
Is there an equivalent in Windows Forms to the Delphi TFrame, or
similar?

Basically I need a way of grouping together some controls in a re-usable
fashion so that I can embed them in various different forms as required.

There must be a way of doing this but I just can't see it.

I'm not a Windows Forms expert, but might Panel be what you're looking
for?
 
J

Jon Skeet [C# MVP]

Sounds like a UserControl. The UserControl will allow you to visually create
(with the help of the visual designer) a control that is composed of other
controls. Once you've created the custom UserControl you're free to add this
control to any container.

I thought UserControl was normally used for things like custom painting
and validation. If positioning and grouping of normal controls is all
that's required, wouldn't Panel do the job slightly more
straightforwardly?
 
T

Toby Groves

Jon Skeet said:
I'm not a Windows Forms expert, but might Panel be what you're looking
for?

Unless I'm mistaken, the Panel won't give me a reusable control that I
can "dunk" on a load of different forms, it merely groups together
controls on a single form.

I think Tim's suggestion about the UserControl is what I'm after, I'll
do a bit more digging on this, thanks to all for the rapid responses :)
 
H

Herfried K. Wagner [MVP]

Toby Groves said:
Is there an equivalent in Windows Forms to the Delphi TFrame, or similar?

Basically I need a way of grouping together some controls in a re-usable
fashion so that I can embed them in various different forms as required.

"Project" -> "Add user control...". User controls are shown in a separate
panel of the toolbox afterwards and can be placed onto forms and other
composite controls.
 
T

Tim Wilson

If I was going to do custom painting I'd probably inherit straight from the
base Control class as it's essentially a plain canvas on which to draw. The
UserControl is a nice way to visually design controls that are typically
made up of other Windows Forms controls. A Panel would work, but does not
give the nice "Form-like" design experience. The "Windows Control Library"
project creates a UserControl and immediately places you into design mode.
Once you've built this control it can be referenced from the Toolbox and
dropped onto a Form, just as any custom control. Overall, the UserControl is
just a little easier to design thanks to the designer support.
 
J

Jon Skeet [C# MVP]

Toby Groves said:
Unless I'm mistaken, the Panel won't give me a reusable control that I
can "dunk" on a load of different forms, it merely groups together
controls on a single form.

I'm not sure I see the difference. A Panel is a Control already... so
just create an instance of your new class derived from Panel, and dunk
it onto a form.

If you're wanting to put a *single* instance onto multiple forms, I
suspect you'll have problems - a control wants to have a single parent,
and things get hairy if you add one control to multiple containers.
I think Tim's suggestion about the UserControl is what I'm after, I'll
do a bit more digging on this, thanks to all for the rapid responses :)

I'm not trying to push my answer as the right one - as I said, I'm not
a WinForms expert - but I'm interested in learning what Panel can't do
for you.
 
J

Jon Skeet [C# MVP]

If I was going to do custom painting I'd probably inherit straight from the
base Control class as it's essentially a plain canvas on which to draw. The
UserControl is a nice way to visually design controls that are typically
made up of other Windows Forms controls. A Panel would work, but does not
give the nice "Form-like" design experience. The "Windows Control Library"
project creates a UserControl and immediately places you into design mode.
Once you've built this control it can be referenced from the Toolbox and
dropped onto a Form, just as any custom control. Overall, the UserControl is
just a little easier to design thanks to the designer support.

Ah, if it's just designer support that's fine. I don't mind not knowing
much about that, not being a fan of the designer to start with :)
 
G

Guest

Hey Toby,
I had the same question when I started using C# after using Delphi since
1995. I used TFrames all the time to switch in and out different views of my
applications. It was very convenient. I was actually suprised when I did
not find an equivalent in .NET. I found the best way to have something
equivalent to a TFrame was to create a new form. This form can be
programmatically added into another form.

Here's a little walkthrough:

- Create a new WinForms project (I use C#).
- Add a panel to the form, sizing it large enough to host the other form.
- Add a button to the form.
- Add a second form to the project. This will be our TFrame.
- Add whatever controls you want on the TFrame form.
- Set the FormBorderStyle of the TFrame form to None.
- Add the following code to the click event of the form on the first form:
Form2 form = new Form2();
form.TopLevel = false;
form.Parent = pnlDock;
form.Show();

This is all you have to do. If you want to keep a reference to the form,
create a private variable at the first form level to be able to close out the
TFrame form. You could also do as in Delphi and set up a base form with all
of the common events and properties created and inherit your TFrame forms
from it. Then you can use the common private variable to reference any of
the inherited form's data.

You can set other properties such as docking and anchoring in the panel to
spruce up the exact position of the form in the panel. If the form is larger
than the panel, you can set the AutoScroll property of the panel to
automatically add scrollbars to the size of the TFrame form.

I hope this helps. If you have any questions, let me know.

Shannon
 
M

mabster

Why go to this trouble rather than just using a UserControl?

UserControls are functionally equivalent to TFrames.
 
G

Guest

You are absolutely correct mabster. But coming from the Delphi world, this
'felt' more like the old TFrame for me than using a UserControl.
Also, when incorporating a form instead of a user control in my app, I could
utlitize the already builtin FormClosing event when the user closed the view
allowing me to do extra processing, or even cancel the close. I realize
there are other ways to do this but for me being able to cancel a close
inside the form itself triggered by a control on the form itself caused less
coding than a user control as the event setup was already there.
 

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