I've put together a sample of a ToolBar control that will allow "docking"
at
both design-time and run-time. By "docking" I mean that the control is
pinned to the edge of it's parent and will be adjusted according to the
parents size. If you really wanted to get fancy you can create a custom
designer that will prevent the user from resizing the control, on the
appropriate edge, by overriding the ControlDesigner's SelectionRules
property. If you paste the code below into the VS.Net editor it might be
easier to read.
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 said:
I am creating a custom toolbar. My toolbar have a property Align. Align put a
toolbar in top, bottom, left ou rigth.
How a get a Parent of my control in design time ?
Ex:
myToolBar.Width = Parent.Width.
Rogerio