Why "GroupBox", "Panel" can contain other Controls ??

S

Stan

How to see if the control is a "container" control ?

Are there any other "container" controls except "GroupBox" and "Panel" ?

Thanks!
 
M

Morten Wennevik

Hi Stan,

Basically all Controls can be containers as Control has a Controls property. You can put a button inside another button if you like. Or a TextBox that has Button with another TextBox inside of it.
 
S

Stan

Hi,

As you mentioned, I'v tried to get "Controls Property" from "Button" but I
can find!?
I'm not very clear about your concept.

My scenario:

1.Put Button1 on form1;
2.Put Button2 over Button1;
3.User "Button2.Parent" to find Button2's parent;
4.I got "form1" , not "Button1"!

If the same scenario but change "Button1" to "Panel1" then I'll get
"Panel1" as Button2's parent.

So if I want to make "Button1" as Button2's parent, how can I do?

Thanks for your kind response!


Morten Wennevik said:
Hi Stan,

Basically all Controls can be containers as Control has a Controls
property. You can put a button inside another button if you like. Or a
TextBox that has Button with another TextBox inside of it.
 
M

Morten Wennevik

I'm not sure what you mean about cannot find the Controls property. However, it is not listed if you just search "Button." in the MSDN "Look For" list, you need to select the Control.

This sample should demonstrate putting a button inside another button.

Button button1 = new Button();
button1.Text = "Button 1";
button1.Size = new Size(100, 100);
form1.Controls.Add(button1);

Button button2 = new Button();
button2.Text = "Button 2";
button1.Controls.Add(button2);
button2.Click += new EventHandler(button2_Click);

....

private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Parent.Text);
}

Note that if you just drag a button over another button in the Designer window, the designer won't actually put the button inside the other button but onto the underlying form. I suppose the designer assumes you don't want Button2 inside Button1.



Hi,

As you mentioned, I'v tried to get "Controls Property" from "Button" but I
can find!?
I'm not very clear about your concept.

My scenario:

1.Put Button1 on form1;
2.Put Button2 over Button1;
3.User "Button2.Parent" to find Button2's parent;
4.I got "form1" , not "Button1"!

If the same scenario but change "Button1" to "Panel1" then I'll get
"Panel1" as Button2's parent.

So if I want to make "Button1" as Button2's parent, how can I do?

Thanks for your kind response!


Morten Wennevik said:
Hi Stan,

Basically all Controls can be containers as Control has a Controls
property. You can put a button inside another button if you like. Or a
TextBox that has Button with another TextBox inside of it.
 
N

news.microsoft.com

I suppose the designer assumes you don't want Button2 inside Button1.

This is true.

You must derive your control from ContainerControl if you want the designer
to allow other controls to be dropped inside of yours.

Like Morten said, all controls can contain Controls inside, the
ContainerControl just offer additional layout functionality.


--
Francisco Padron
www.chartfx.com


Morten Wennevik said:
I'm not sure what you mean about cannot find the Controls property.
However, it is not listed if you just search "Button." in the MSDN "Look
For" list, you need to select the Control.

This sample should demonstrate putting a button inside another button.

Button button1 = new Button();
button1.Text = "Button 1";
button1.Size = new Size(100, 100);
form1.Controls.Add(button1);

Button button2 = new Button();
button2.Text = "Button 2";
button1.Controls.Add(button2);
button2.Click += new EventHandler(button2_Click);

...

private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Parent.Text);
}

Note that if you just drag a button over another button in the Designer
window, the designer won't actually put the button inside the other button
but onto the underlying form. I suppose the designer assumes you don't
want Button2 inside Button1.
 
N

news.microsoft.com

All controls can parent another control. However the design time support is
available only for few of them.

I'm not sure that the suggestion you've got, to inherit from
ContainerControl, will help you in this matter. What you need to do is to
create your own designer that inherits from ParentControlDesigner and attach
this designer to that control of yours.


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

Francisco Padron

Deriving from ContainerControl is enough to allow the designer to drop other
controls inside of your control.

By deriving from ContainerControl you are automatically attaching a
ScrollableControlDesigner to your control. ScrollableControlDesigner
derives from ParentControlDesigner. So you are basically doing the same
thing (in regards to this topic).

One important point that Stoitcho brought is that having a designer that
derives from ParentControlDesigner is what makes it work (not the fact that
you derive from ContainerControl), so if you have a custom designer you MUST
derive it from ParentControlDesigner.
 
S

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

Francisco,

I agree that via inheriting from ContainerControl we add the design time
functionality for hosting other control from the toolbox, However
ScorllableControlDesigner is not good enough. It doesn't draw the border of
the control, so the control is not visible unless DrawGrid is set to
*false*. It is better in this case to inherit from Panel class. The only
things Panel's designer does is to add that piece of code that draws the
border if the panel doesn't have its own.

However, I believe if one wants to create a new container control with
design support one must want to have something more that already provided
functionality. This is the major reason for me to believe that inhertiting
from ContainerControl is not what Stan was after.


Stoitcho Goutsev (100) [C# MVP]
 

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