65.9K
CodeProject is changing. Read more.
Home

Custom Windows Panel

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.69/5 (19 votes)

May 29, 2009

CPOL
viewsIcon

66117

downloadIcon

1887

Every control in this panel can change the size and move on the panel

Introduction

I've downloaded some articles to use in my project for resizing and moving every control at runtime, but those aren't more efficient in my conditions. So I decided to make a custom panel to do that. It's useful for some projects such as report generator and more.

Using the Code

Canvas class is inherited from Panel control and in the OnControlAdded method of the panel, some events are used to handle moving controls.

Canvas has defined Tracker class to handle resizing the control on the panel.

Just drag and drop the custom panel (named Canvas) on the form and put some controls to test it.

public class Canvas : Panel
{
...
protected override void OnControlAdded(System.Windows.Forms.ControlEventArgs e)
{
base.OnControlAdded(e);
if (e.Control is Tracker)
return;

e.Control.MouseEnter += new EventHandler(Control_MouseEnter);
e.Control.MouseLeave += new EventHandler(Control_MouseLeave);
e.Control.MouseDown += new MouseEventHandler(Control_MouseDown);
e.Control.MouseMove += new MouseEventHandler(Control_MouseMove);
e.Control.MouseUp += new MouseEventHandler(Control_MouseUp);

e.Control.LocationChanged += new EventHandler(Control_LocationChanged);

e.Control.HandleDestroyed += new EventHandler(Control_HandleDestroyed);
}
...
}

History

  • 29th May, 2009: Initial post
  • 15th Mar, 2010: Updated source code