How a get a Parent of my control in design time ?
Ex:
myToolBar.Width = Parent.Width.
Rogerio
"Rogerio Jun" <Roger...@discussions.microsoft.com> wrote in message
news:1660D7B1-6432-451A...@microsoft.com...
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
#region Designer
============================================================================
======
#if DESIGN
[assembly:
System.CF.Design.RuntimeAssemblyAttribute("MyCompany.Windows.Forms.ToolBarEx
, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")]
#endif
#endregion
============================================================================
============
namespace MyCompany.Windows.Forms
{
public enum DockStyle
{
None = 0x00,
Top = 0x01,
Bottom = 0x02,
Left = 0x03,
Right = 0x04
}
public class ToolBarEx : System.Windows.Forms.Control
{
private MyCompany.Windows.Forms.DockStyle dock =
MyCompany.Windows.Forms.DockStyle.Top;
#if DESIGN
#region Designer
============================================================================
[
CategoryAttribute("Layout"),
DescriptionAttribute("Indicates the docking location of the control."),
DefaultValueAttribute(MyCompany.Windows.Forms.DockStyle.Top)
]
public new MyCompany.Windows.Forms.DockStyle Dock
#endregion
============================================================================
======
#else
public virtual MyCompany.Windows.Forms.DockStyle Dock
#endif
{
get
{
return dock;
}
set
{
if (dock != value)
{
dock = value;
UpdateDock();
}
}
}
#if DESIGN
#region Designer
============================================================================
protected override Size DefaultSize
#endregion
============================================================================
======
#else
protected virtual Size DefaultSize
#endif
{
get
{
return new Size(26, 26);
}
}
public ToolBarEx() { }
protected override void OnParentChanged(System.EventArgs e)
{
if (this.Parent != null)
{
UpdateDock();
this.Parent.Resize += new EventHandler(Parent_Resize);
}
base.OnParentChanged(e);
}
private void Parent_Resize(object sender, EventArgs e)
{
UpdateDock();
}
private void UpdateDock()
{
#region Adjust Bounds
====================================================================
if (this.Parent != null)
{
Size defaultSize = this.DefaultSize;
Size parentSize = this.Parent.ClientSize;
switch (this.Dock)
{
case DockStyle.None:
{
// Do nothing.
break;
}
case DockStyle.Top:
{
this.Bounds = new Rectangle(0, 0, parentSize.Width, defaultSize.Height);
break;
}
case DockStyle.Bottom:
{
this.Bounds = new Rectangle(0, (parentSize.Height - defaultSize.Height),
parentSize.Width, defaultSize.Height);
break;
}
case DockStyle.Left:
{
this.Bounds = new Rectangle(0, 0, defaultSize.Width, parentSize.Height);
break;
}
case DockStyle.Right:
{
this.Bounds = new Rectangle((parentSize.Width - defaultSize.Width), 0,
defaultSize.Width, parentSize.Height);
break;
}
}
}
#endregion
============================================================================
===
}
}
}
--
Tim Wilson
.Net Compact Framework MVP
"Rogerio Jun" <Roger...@discussions.microsoft.com> wrote in message
news:1660D7B1-6432-451A...@microsoft.com...
Am I right?
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:eoIiHhBN...@TK2MSFTNGP09.phx.gbl...
--
Tim Wilson
.Net Compact Framework MVP
"MacFar" <bla...@blablabla.com> wrote in message
news:OIJhMlCN...@TK2MSFTNGP10.phx.gbl...
private Control parent = null;
...
protected override void OnParentChanged(System.EventArgs e)
{
EventHandler handler = new EventHandler(Parent_Resize);
if (this.parent != null)
{
this.parent.Resize -= handler;
}
if (this.Parent != null)
{
UpdateDock();
this.Parent.Resize += handler;
}
this.parent = this.Parent;
base.OnParentChanged(e);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (this.parent != null)
{
this.parent.Resize -= new EventHandler(Parent_Resize);
}
}
base.Dispose(disposing);
}
--
Tim Wilson
.Net Compact Framework MVP
"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:%23196p2F...@TK2MSFTNGP09.phx.gbl...
What is wrong ?
"Tim Wilson" wrote:
> You shouldn't get an error message when you remove the control if you check
> whether the Parent is null in the OnParentChanged method, as in the code
> sample. The problem is that the control can be reparented which means that
> you are not really given the opportunity to remove the handler since the
> OnParentChanged method will be called once with "null", when the control is
> removed from the original parent, and once with the new parent when it is
> reparented. So removing in the Dispose method will work but only for the
> last control that was acting as a parent. But I agree - as good practice you
> should remove from an event handler that you no longer need.
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
--
Tim Wilson
.Net Compact Framework MVP
"Rogerio Jun" <Roger...@discussions.microsoft.com> wrote in message
news:EA5B6FB8-F5A6-48B1...@microsoft.com...