Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Custom Windows Panel

4.69/5 (20 votes)
15 Mar 2010CPOL 65.9K   1.9K  
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.

C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)