Compound Control as Parent Control / ParentControlDesigner / Controls Collection

Compound Control as Parent Control / ParentControlDesigner / Controls Collection

Post by Harry von Borste » Thu, 21 Feb 2002 23:22:12



Hi,

How can I manage this:

MyParentControl has some constituent controls (label1, text1 and panel1). I
want to act it as a parent control: the user should be able to drag other
controls on it at design time. These other controls should be placed on
panel1.

These are my experiments:

(*) 1. Approach:
Put this line in front of the class:
[Designer(typeof(System.Windows.Forms.Design.ParentControlDesigner))]
public class MyParentControl...

All works fine, but the user's controls are mixed with the constituent
controls into MyParentControls.Controls. I have to panel1.SendToBack in
order to make the user's controls visible.

(*) 2.  Approach:
Overwrite MyParentControl.CreateControlsInstance to feed it with a special
ControlsCollection that has an Add method which sends the user's controls to
panel1.Controls

Now the user's controls are parented by panel1. I can click them at design
time and edit their properties through the properties window. But they don't
get the adornments that normally enables you to move and resize selected
controls. Then I wrote a special MyParentControlDesigner, inherited from
ParentControlDesigner, and put the adornment-drawing code in its
OnPaintAdornments method. Looks pretty well good, but the adornments are
still dead. I had to add drag-and-drop code. After a short look through
ildasm at the code of TabControl this seemed a lot of work.

(*) 3. Approach:
Found a thread in this NG from Russ Morris and Shawn Burke
"ControlDesigners: Persisting in code the children of custom
ControlContainers" starting July 14.
Adding some code like
public new Control.ControlCollection Controls
{
  put {
    if(mInitializing)
      return base.Controls;
    else
      return panel1.Controls;
  }

Quote:}

This new Controls property is completely ignored by the design-time
environment, it seems to call (Control)Controls, not
(MyParentControl)Controls. The design-time behavior is exactly like that of
the first approach.

Any suggestions? Any help is greatly appreciated!

Best regards
- Harry

 
 
 

Compound Control as Parent Control / ParentControlDesigner / Controls Collection

Post by Shawn Burke [MSFT » Wed, 27 Feb 2002 04:18:40


As as starting point, I think #1 is your best bet.  Changing the designer on
your MyParentControl to ParentControlDesigner is the right thing to do, but
it makes that control eligable for accepting parents, not the panel inside
of it.   Moreover, if you're forcing those newly added controls to be
parented by that panel, you are probably confusing the designer.  The
ControlDesigner generally expects it's parent to be another sited component
in the designer.

See, the Panel iteself isn't sited so the designer doesn't know about it.
When you add controls, you're going to be adding them to the control the
designer knows about which is the UserControl itself.  That's why you have
to keep moving the panel back, because the new contols are being added
underneath it.  There isn't much of a way around this.  Reparenting them
manually is going to confuse the designer and probably break code
generation.  You should consider not using the Panel and instead parenting
directly to your control

--

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. ? 2001 Microsoft Corporation. All rights
reserved.



Quote:> Hi,

> How can I manage this:

> MyParentControl has some constituent controls (label1, text1 and panel1).
I
> want to act it as a parent control: the user should be able to drag other
> controls on it at design time. These other controls should be placed on
> panel1.

> These are my experiments:

> (*) 1. Approach:
> Put this line in front of the class:
> [Designer(typeof(System.Windows.Forms.Design.ParentControlDesigner))]
> public class MyParentControl...

> All works fine, but the user's controls are mixed with the constituent
> controls into MyParentControls.Controls. I have to panel1.SendToBack in
> order to make the user's controls visible.

> (*) 2.  Approach:
> Overwrite MyParentControl.CreateControlsInstance to feed it with a special
> ControlsCollection that has an Add method which sends the user's controls
to
> panel1.Controls

> Now the user's controls are parented by panel1. I can click them at design
> time and edit their properties through the properties window. But they
don't
> get the adornments that normally enables you to move and resize selected
> controls. Then I wrote a special MyParentControlDesigner, inherited from
> ParentControlDesigner, and put the adornment-drawing code in its
> OnPaintAdornments method. Looks pretty well good, but the adornments are
> still dead. I had to add drag-and-drop code. After a short look through
> ildasm at the code of TabControl this seemed a lot of work.

> (*) 3. Approach:
> Found a thread in this NG from Russ Morris and Shawn Burke
> "ControlDesigners: Persisting in code the children of custom
> ControlContainers" starting July 14.
> Adding some code like
> public new Control.ControlCollection Controls
> {
>   put {
>     if(mInitializing)
>       return base.Controls;
>     else
>       return panel1.Controls;
>   }
> }

> This new Controls property is completely ignored by the design-time
> environment, it seems to call (Control)Controls, not
> (MyParentControl)Controls. The design-time behavior is exactly like that
of
> the first approach.

> Any suggestions? Any help is greatly appreciated!

> Best regards
> - Harry


 
 
 

Compound Control as Parent Control / ParentControlDesigner / Controls Collection

Post by Harry von Borste » Thu, 28 Feb 2002 23:32:17


Hi Shawn,

Thank you very much for your reply. The hint that any component has to be
parented by a sited component pointed me to the right direction.

The #1 approach was not suitable in the reality, because that is not the
"natural" windows arragentment for my case. The user's controls should be
hidden, when panel1 is hidden, they should move along with panel1, they
should use the AutoScroll feature (btw. this is a great one!) etc.

Now I tried a 4th approach: Adding the panel at user's design-time.

1. Using a designer MyParentControlDesigner inherited from
ParentControlDesigner for MyParentControl

2. Adding an internal field to MyParentControl:
internal Panel panel = null;

3. Adding this to MyParentControlDesigner:
private void Panelize()
{
  MyParentControl a = (MyParentControl)Control;
  if (a.panel==null)
    {
      a.panel = new Panel();
      IContainer c;
      c = this.Component.Site.Container;
      c.Add(a.panel);
      a.Controls.Add(a.panel);
    }
  a.panel.BringToFront();

Quote:}

After these 3 steps and after the designer had called Panelize() I had an
environment that made it possible to drag other controls onto the panel.
Serializing works like this:
this.MyParentControl1.Controls.AddRange(new System.Windows.Forms.Control[]
{this.panel1});
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[]
{this.control1,this.control2});

The rest was (and is still) fine-tuning:

4. Override MyParentControlDesigner.CanParent:
public override bool CanParent(System.Windows.Forms.Control control)
{
  if (control is Panel)
    return true;
  else
  {
    Panelize();
    return false;
   }

Quote:}

This has two effects: It prevents other controls than Panel to be drawn
directly to MyParentControl and it panelizes MyParentControl if neccessary.
When I'm dragging a control from the form (not from the toolbox!) to
MyParentControl the panel is added "on the fly" and it's just in time ready
to accept the dragged control instead of MyParentControl.

Unfortunately it seems that CanParent is not called when a control is
dragged from the toolbox. I don't have a perfect solution for this situation
(any suggestions?), this is what I'm doing now:

5. Override MyParentControlDesigner.OnSetCursor:
protected override void OnSetCursor()
{
  Panelize();
  base.OnSetCursor();

Quote:}

Best regards

Harry



> As as starting point, I think #1 is your best bet.  Changing the designer
on
> your MyParentControl to ParentControlDesigner is the right thing to do,
but
> it makes that control eligable for accepting parents, not the panel inside
> of it.   Moreover, if you're forcing those newly added controls to be
> parented by that panel, you are probably confusing the designer.  The
> ControlDesigner generally expects it's parent to be another sited
component
> in the designer.

> See, the Panel iteself isn't sited so the designer doesn't know about it.
> When you add controls, you're going to be adding them to the control the
> designer knows about which is the UserControl itself.  That's why you have
> to keep moving the panel back, because the new contols are being added
> underneath it.  There isn't much of a way around this.  Reparenting them
> manually is going to confuse the designer and probably break code
> generation.  You should consider not using the Panel and instead parenting
> directly to your control

> --

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> You assume all risk for your use. ? 2001 Microsoft Corporation. All rights
> reserved.



> > Hi,

> > How can I manage this:

> > MyParentControl has some constituent controls (label1, text1 and
panel1).
> I
> > want to act it as a parent control: the user should be able to drag
other
> > controls on it at design time. These other controls should be placed on
> > panel1.

> > These are my experiments:

> > (*) 1. Approach:
> > Put this line in front of the class:
> > [Designer(typeof(System.Windows.Forms.Design.ParentControlDesigner))]
> > public class MyParentControl...

> > All works fine, but the user's controls are mixed with the constituent
> > controls into MyParentControls.Controls. I have to panel1.SendToBack in
> > order to make the user's controls visible.

> > (*) 2.  Approach:
> > Overwrite MyParentControl.CreateControlsInstance to feed it with a
special
> > ControlsCollection that has an Add method which sends the user's
controls
> to
> > panel1.Controls

> > Now the user's controls are parented by panel1. I can click them at
design
> > time and edit their properties through the properties window. But they
> don't
> > get the adornments that normally enables you to move and resize selected
> > controls. Then I wrote a special MyParentControlDesigner, inherited from
> > ParentControlDesigner, and put the adornment-drawing code in its
> > OnPaintAdornments method. Looks pretty well good, but the adornments are
> > still dead. I had to add drag-and-drop code. After a short look through
> > ildasm at the code of TabControl this seemed a lot of work.

> > (*) 3. Approach:
> > Found a thread in this NG from Russ Morris and Shawn Burke
> > "ControlDesigners: Persisting in code the children of custom
> > ControlContainers" starting July 14.
> > Adding some code like
> > public new Control.ControlCollection Controls
> > {
> >   put {
> >     if(mInitializing)
> >       return base.Controls;
> >     else
> >       return panel1.Controls;
> >   }
> > }

> > This new Controls property is completely ignored by the design-time
> > environment, it seems to call (Control)Controls, not
> > (MyParentControl)Controls. The design-time behavior is exactly like that
> of
> > the first approach.

> > Any suggestions? Any help is greatly appreciated!

> > Best regards
> > - Harry

 
 
 

1. How to control the Parent Web Page Controls from the Child Web Control

Sorry for the bad subject description.
Please use vb examples, not C#.

I've got a Login Web User Control inside a Default page.
When I login from the form located on the Web User Control,
I would like to set the login hyperlink button's visibility to false
and the logout hyperlink button's visibility to true.

I tried using the following code, but it didn't work.

Dim oLink As HyperLink = CType(Me.Parent.FindControl("lnk_login"),
HyperLink)
oLink.Visible = False

Thanks
Info Cloud

2. Frontpage 2002 DHTML Mouse rollover

3. How to prevent controls of a collection being inserted in Controls property when control is pasted?

4. Need Help Linking Device Driver

5. Controls created on one thread cannot be parented to a control on a different thread.

6. Urgent required oracle/PL Sql developer - Jersey City, NJ

7. Controls created on one thread cannot be parented to a control on a different th

8. Nokia 6210 + Win2K + IR

9. How can a parent control draw on child controls?

10. Reverse association, from separate control to parents controls.

11. Painting a Controls's Parent onto the Control himself

12. Accessing Other User Controls and Parents From User Control

13. Accessing this.Parent.Controls from Custom Control